linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: "David Hildenbrand (Red Hat)" <david@kernel.org>
To: Kevin Lourenco <klourencodev@gmail.com>
Cc: linux-mm@kvack.org, akpm@linux-foundation.org,
	Kevin Lourenco <k.lourenco@criteo.com>
Subject: Re: [PATCH] mm/fadvise: validate offset in generic_fadvise
Date: Tue, 6 Jan 2026 20:46:14 +0100	[thread overview]
Message-ID: <1690da61-412a-457a-9bb4-1135838135da@kernel.org> (raw)
In-Reply-To: <CAFveykO9tAhGnKCnSD-2_wt3QxMTNF3sW20NGwnPK-3yMd__yA@mail.gmail.com>

On 12/23/25 17:16, Kevin Lourenco wrote:
> Hi David,

Hi!

> 
> Thank you for your thoughts and recommendations. Well noted, I will
> now start limiting the commit message to those specifications :)
> 
> For point (1):
> 
> I see POSIX definition like this: "The byte position in the file where
> the next I/O operation begins."
> (https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html?utm_source=chatgpt.com),
> so what is almost certain is that a negative offset makes no sense /
> there is no hidden mechanism or anything.

All I find in that regard is

"... starting at offset and continuing for len bytes. The specified 
range need not currently exist in the file. If len is zero, all data 
from offset to the largest possible value of the file offset for that 
file shall be specified. "

So, something with a negative offset sure does not exist in the file.

Which makes me wonder whether we are here in a scenario where we would 
simply ignore everything "negative" (clamp offset to 0), simply return 0 
(because it's not documented to create an error), or whether we are in 
undefined behavior land and can simply return EINVAL.

> I don't think we can
> accidentally break users, my intuition is that nobody ever
> intentionally passes a negative offset (probably the same for len).

Some programs do stupid things, you would be surprised :)

> 
> We could rely exclusively on the user to ensure the offset is positive
> and follow POSIX recommendation. But since errors can occur, I think
> it can be hard to debug where a simple test, the same for len, could
> have made their experience easier. I think this is also the path
> FreeBSD chose: https://github.com/freebsd/freebsd-src/blob/main/sys/kern/vfs_syscalls.c#L4918

That's very good thing to note!

Interestingly, they also check

	offset > OFF_MAX - len

So checking that offset+len will not overflow. Do we want something 
similar? (check_add_overflow() etc. )

> 
> For point (2):
> 
> Since I think nobody intentionally passes negative values and since
> that makes no sense semantically speaking, I think we can consider it

Well, then why document negative length? ;) We should be as clear as 
possible in the documentation.

If POSIX would have been clearer we wouldn't have this discussion :)

> unnecessary to update the man page right now and only think about it
> in the future if a negative offset gains a new meaning, for example.
> 
> Wdyt?

Likely we should really update the man page to reflect reality. 
"Starting with Linux v7.0, posix_fadvise() will fail with ...".

Given that FreeBSD rejects negative offsets I guess we are good.

-- 
Cheers

David


      reply	other threads:[~2026-01-06 19:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-22 14:18 klourencodev
2025-12-22 17:52 ` Andrew Morton
2025-12-22 23:38   ` Kevin Lourenco
2025-12-23  9:45 ` David Hildenbrand (Red Hat)
2025-12-23 16:16   ` Kevin Lourenco
2026-01-06 19:46     ` David Hildenbrand (Red Hat) [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1690da61-412a-457a-9bb4-1135838135da@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=k.lourenco@criteo.com \
    --cc=klourencodev@gmail.com \
    --cc=linux-mm@kvack.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox