The Linux kernel will fix some peculiar argv usage in execve(2)
utcc.utoronto.ca
utcc.utoronto.ca
* Someone suggests that the kernel return EINVAL for null or zero-length argument vector. Someone else then comes up with a mad but very real mainstream program that relies upon the system call succeeding.
* Someone points out that the SUS requires that argv[0] be non-null. Someone else tries to weasel a difference between "shall" and "should", overlooking the SUS rationale that the leeway given is in the string contents, not that it is permitted to be outright null.
* Someone suggests in all seriousness that this behaviour be retained for historical compatibility.
For reference:
* FreeBSD just returns EINVAL for a zero-length argument vector, https://github.com/freebsd/freebsd-src/commit/773fa8cd136a57... . This came from OpenBSD.
* A null argument vector has been EFAULT in FreeBSD since 2004, when someone noticed that the manual disallowed this, https://github.com/freebsd/freebsd-src/commit/7700eb86e7740c... .
* DragonFly BSD has been fixing up a zero-length argument vector by adding in a dummy non-null argv[0] since 2005, https://github.com/DragonFlyBSD/DragonFlyBSD/commit/66be6566... . It introduced EFAULT for a null argument vector at the same time.
* Illumos has returned EFAULT for a null argument vector since at least the point when it went open-source in 2005.
It's worth noting that being correct according to the SUS would have avoided the problem, since the SUS calls out the applications-code assumptions that cause the vulnerabilities in its rationale.
The question here is: will anyone notice the breakage? And is this a serious enough issue to risk it?
This is the idea, but the Kernel maintainers aren’t omniscient, so “no one would notice” really means “the maintainers don’t think anyone would notice”, and this ends up being somewhat arbitrary.
A breaking change is a breaking change: just because you don’t think anyone is relying on the existing behaviour doesn’t make it any less of a breaking change.
I really don’t like it when Linus says the Kernel never breaks userspace (he says this a lot and uses it as an excuse to abuse people on the mailing list) because it’s just factually inaccurate, and “no one will notice” isn’t really good enough if you expect people to actually rely on this property.
The fact it sometimes need to be broken if it is unfixably insecure is obvious conclusion for anyone that thinks about the problem instead of looking for reasons to complain
Yeah, that's pretty much what I meant, but this is phrased better than I did.
I do think that "Kernel never breaks userspace" is accurate in the common-understanding sense and a good policy to have, even though it's indeed not accurate in the sense that it's "100% technically accurate" (Linux also just breaks things by accident for example).
You could use a qualified statement like "Linux takes great care not to break userspace, except when there is a overwhelming case in favour of it, or when we think no real applications will break with it, and we don't always roll back accidental breakage if we don't think it's worth it" and that would be more accurate, but that's rather vague, and a clear "never break userspace" is a much clearer better as a policy for everyone.
In interviews and such Linus is of course much more nuanced than "never break userspace".
I think given some of Linus’s LKML rants directed at people who have broken userspace, it’s reasonable to take the claim at face value and assume that there is a hard guarantee of backwards compatibility (modulo bugs).
I'm not fan of his outbursts by any means, but you have to see these things in context. Imagine everything you post on the internet will be read by heaps of people and still be quoted years from now. Often my posts on e.g. HN don't express my full nuanced views either, because it's not germane to the topic at hand, because I phrased things badly, and sometimes I'm just dead wrong. No one quotes all the stupid stuff I said 15 years ago though, or the stupid stuff I said yesterday.
Few months ago I wrote a feature request and patch to GNU coreutils that resulted in some discussion of this NULL argv issue.
https://lists.gnu.org/archive/html/coreutils/2023-03/msg0000...
https://lists.gnu.org/archive/html/coreutils/2023-03/msg0000...
https://lists.gnu.org/archive/html/coreutils/2023-03/msg0001...
> An edge case is we may want to support allowing to specify a NULL argv[0].
So the GNU developers are aware of this issue. Perhaps they make use of it in other GNU software. Not sure.
It seems my patch wasn't accepted to the coreutils. I offered to add support for NULL argv, they said they were deciding what to do but I never got a reply. So env doesn't have this functionality yet.
Thanks, you saved me some looking, I thought OpenBSD did something in this case and was going to poke around.
To bad the BSDs and Linux could not agree on what to do :) But really, for what I develop this is a non issue.
I find this sentiment surprising coming from someone pedantic enough to have written:
https://jdebp.uk/FGA/han-unification.html
I disagree. For "can", in Russian you would use the verb мочь (in the case of your example: я могу сходить в туалет?, lit. "I can / am able to go to the bathroom?"); for "may", you would use the predicative можно (in the case of your example: можно сходить в туалет?, lit. "is one allowed / is it possible to go to the bathroom?").
Mandatory xkcd reference https://xkcd.com/1172/
Indeed, the test suite is, rather, this very bug waiting to happen. Still. Two years later.
https://git.kernel.org/pub/scm/fs/xfs/xfstests-dev.git/tree/...
Easily solved by:
static char *argv[] = {
FILE1,
NULL,
};
The xfs test program invoked by the execve() is calling getopt_long_only() passing the zero argc and a zero-length argv that execve() was given. It is quite ironic that that test program is thus exceeding the argument vector bounds and going off into some adjacent part of memory, because one of the first things that getopt_long_only() does is: optind = 1; /* Don't scan ARGV[0], the program name. */I don't think it was deliberate, I suspect they may have switched libc or something.
Input validation, unsound assumptions, relying on magic values (NULL termination)
Boring stuff still causes damage
It also shows why C is such a hard programming language. There is nothing to help the programmer in this case. The compiler doesn't know that argc is the length of argv, so there is not even a warning if you do it wrong.
Quoting ISO C17:
> argv[argc] shall be a null pointer.
Not constrained on argc being greater than zero (unlike the points that follow).
So argv should always be dereferenceable within the program. It's an other question if this should be enforced by the Linux kernel or the C runtime.
Now if the Linux kernel doesn't promise to pass a non-NULL argv (and documents so), then it should be the responsibility of the C runtime to fix that once the program reaches "main". But as I wrote earlier, it's a whole other question.
> [under int main (int argc, char *argv[]);] The argv and environ arrays are each terminated by a null pointer. The null pointer terminating the argv array is not counted in argc.
This confirms that the argv function parameter of main cannot be null.
I couldn't find the behavior specified for passing null for argv in exec*. As other comments pointed out some BSDs return with EINVAL. I think both this and Linux's behavior is compliant.
[0] https://pubs.opengroup.org/onlinepubs/9699919799/functions/e...
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V...
The Single Unix Specification says, very clearly:
> The argument arg0 should point to a filename string that is associated with the process being started by one of the exec functions.
> The value in argv[0] should point to a filename string that is associated with the process being started by one of the exec functions.
Then it goes on at length in the rationale at the bottom of that same section about "the use of the word should" and even explains how programs are tripped up by argc being 0.
As you can see, both masfuerte and planede have still clearly not read this, despite that it's said twice in the description, and then has two entire paragraphs devoted to the reasoning underpinning it in the rationale.
> The meanings specified in POSIX.1-2017 for the words shall, should, and may are mandated by ISO/IEC directives.
By those directives "should" indicates recommendation. [1]
> In POSIX.1-2017, the word should does not usually apply to the implementation, but rather to the application. Thus, the important words regarding implementations are shall, which indicates requirements, and may, which indicates options.
You also have to differentiate between any requirements for the argv argument when passed to one of the exec functions and the argv parameter received in main. Any requirement for the former constrains the application, but requirements for the latter constrains the implementation.
exec is indeed only documented in POSIX and OS specific documentations, but main is also documented in ISO C. Both POSIX and ISO C constrains the argv parameter of main not to be null. They don't require argc > 0 or argv[0] != null.
From [2]:
> [under exec] The argument argv is an array of character pointers to null-terminated strings.
This is a hard requirement, without "should". This implies that argv must not be null itself when passed to exec. This probably gives implementations the freedom to do whatever in this case, POSIX doesn't define the behavior. The requirement for argv[0] (which implies argc > 0) is merely a recommendation.
[0] https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xbd...
[1] https://www.iso.org/foreword-supplementary-information.html
Yes, that spec places additional requirements on top. But looking at just the C spec is apparently enough to find a violation here, and that's relevant! (Especially because the C spec doesn't say "should".)
People wanting to point that error out doesn't mean they're failing to grasp anything.
> argv[argc] shall be a null pointer.
constraint that that violates. argv[argc] is a null pointer. You have not found a violation of that constraint.
The constraint that is violated is, rather, in the Single Unix Specification. It's the SUS that puts the constraint upon how execve() may be invoked by a strictly conformant application, and requires that (as it says twice) argc be "one or greater", because it constrains the first element of the argument vector to be a pointer to a string, not a null pointer. The _only_ leeway is the wiggle room in "associated with", which it devotes an entire paragraph to explaining.
More than one standard applies. The idea that only the C standard covers the writing of these programs is wholly wrongheaded. After all, the C standard also allows non-POSIX implementations.
One scenario is that argc is 0 and argv[0] is NULL.
The other scenario is that argc is 0, argv is NULL, and argv[0] does not exist.
The C standard makes the second scenario invalid, but Linux was allowing it.
This is all directly relevant to the first two posts in the comment thread, talking about "relying on magic values (NULL termination)" and "Obviously, if argc == 0, you should not dereference anything in argv. [...] shows why C is such a hard programming language". Even though they were talking about C by itself, that's actually incorrect, C guarantees the null termination and this rule was being broken by the OS.
I'd say that's not obvious. Normally argv is null-terminated so you can read argv[argc], and it's zero. There's no need to read argv[argc] if you've already looked at argc but sometimes it's more convenient to rely on the sentinel value.
Yes, that null has to be there for historical reasons. It doesn't mean it is a good idea to write code that relies on it.
argv[argc] shall be a null pointer
So I wouldn't feel too bad about relying on it.
That null has to be there because if it isn't then the implementation is broken, not the program.
If you are coding defensively because the implementation might be broken, then you can't rely on argc being positive either
Whenever I see
>while char != NULL
Im a little bit sad. It doesnt feel robust.
Note that the buggy code behind CVE-2021-4034 literally uses argc, not null termination.
Rejected as "documented" in 2007. Also mentions a similar issue with envp.