21 ms·
Handling argc==0 in the Linux kernel
- dtech 5y agoI'm continually (positively) amazed at the commitment to backwards compatibility from the Linux kernel team and continually abhorred by the number of footguns in C/POSIX/Linux.
- mschuster91 5y agoAt least for the old POSIX stuff this is sort of excusable... it operated under very much more relaxed assumptions, primarily that people could trust each other to a somewhat large degree. Obviously, the rise of mass-population Internet access and financial (or "lulz") incentives for hackers changed the game, and we're still paying for all the decisions to be backwards-compatible, even decades later.
- pjmlp 5y agoThe Morris worm already shown what the future would look like in the early 80's.
- deleted 5y ago[deleted]
- bch 5y agoI think the spec has not meaningfully changed in this regard since C89, and there’s a case of collective false memories[0] happening here. From a copy[1] of C89 I see the relevant description as: * The value of argc shall be nonnegative. * argv[argc] shall be a null pointer. So the spec is coherent and explained plainly. Developers/application code just needs to be mindful of what/how it’s dereferencing. [0] https://www.discovermagazine.com/mind/collective-false-memories-whats-behind-the-mandela-effect https://www.discovermagazine.com/mind/collective-false-memor... [1] http://port70.net/~nsz/c/c89/c89-draft.html http://port70.net/~nsz/c/c89/c89-draft.html
- jandrese 5y ago0 is a nonnegative number.
- bch 5y ago> 0 is a nonnegative number. Correct. So it needs to be accounted for.
- deleted 5y ago[deleted]
- tedunangst 5y agoThere is some additional commentary in the rationale section of posix. https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html https://pubs.opengroup.org/onlinepubs/9699919799/functions/e...
- bch 5y agoThis is pretty interesting - some tension between what C guarantees and what Strictly Conforming POSIX requires. I’m not saying there’s no confusion in the real world (it’s obvious there is). Is Postel’s Law Be conservative in what you send; be liberal in what you accept. relevant here? I’m not sure. The interesting looking parts of @tedunangst linked document are: The argument argv is an array of character pointers to null-terminated strings. The application shall ensure that the last member of this array is a null pointer. These strings shall constitute the argument list available to the new process image. 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. […] Early proposals required that the value of argc passed to main() be "one or greater". This was driven by the same requirement in drafts of the ISO C standard. In fact, historical implementations have passed a value of zero when no arguments are supplied to the caller of the exec functions. This requirement was removed from the ISO C standard and subsequently removed from this volume of POSIX.1-2017 as well. The wording, in particular the use of the word should, requires a Strictly Conforming POSIX Application to pass at least one argument to the exec function, thus guaranteeing that argc be one or greater when invoked by such an application. In fact, this is good practice, since many existing applications reference argv[0] without first checking the value of argc. The requirement on a Strictly Conforming POSIX Application also states that the value passed as the first argument be a filename string associated with the process being started. Although some existing applications pass a pathname rather than a filename string in some circumstances, a filename string is more generally useful, since the common usage of argv[0] is in printing diagnostics. In some cases the filename passed is not the actual filename of the file; for example, many implementations of the login utility use a convention of prefixing a <hyphen-minus> ( '-' ) to the actual filename, which indicates to the command interpreter being invoked that it is a "login shell".
- im3w1l 5y agoI don't think the behavior is wrong per se. Rather the issue is that it was tribal knowledge that the way you got the name of your binary was through argv[0]. And that this misinformation spread unchecked.
- staticassertion 5y agoIt seems straightforward to just enumerate every setuid program that ships on the vast majority of distros and check for this issue and patch what's there. That seems like the simplest approach with no concern for breaking anything. Also, a sysctl for rejecting the call would seem reasonable.
- hedora 5y agoAgreed. Also, the existence of this bug makes me wonder if I can simply delete polkit. What other rookie mistakes lie within? I learned how to parse argv properly in CS 50 (so, before CS 101).
- rwmj 5y agopkexec was a kludge to replace consolehelper and both were actually supposed to have been removed in 2013(!) but the transition didn't happen fully. In fact some are still using consolehelper. For the whole sorry tale, https://lwn.net/Articles/883547/ https://lwn.net/Articles/883547/ https://lwn.net/SubscriberLink/883547/f9a8f4e4be157f91/ https://lwn.net/SubscriberLink/883547/f9a8f4e4be157f91/
- fulafel 5y agoInteresting. Ironically the reason pkexec exists on Ubuntu server installs seems to be that they want to have a package management service (PackageKit) hanging off dbus even in headless setups, for reasons that aren't obvious. There have been earlier local root bugs caused by PackageKit as well[1]. (The irony being, the LWN documented discussion where a dev defends ipc-accessible privileged daemons as more secure and justifies pkexec as facilitating access to these). [1] https://ubuntu.com/security/notices/USN-4538-1 https://ubuntu.com/security/notices/USN-4538-1
- simcop2387 5y agoI believe it gets used for the unattended-upgrades system to manage security updates. Not sure if that's the best way to do it but I believe it's using packagekit to do the actual work.
- SiebenHeaven 5y ago"We do not break the userland"
- deleted 5y ago[deleted]
- kardos 5y agoIndeed. But a sysctl to switch the patch on/off seems like the pragmatic solution
- josefx 5y agoThat isn't pragmatic, it silently breaks programs that rely on specified behavior just to fix one of many self inflicted security issues polkit had over the last decade.
- colejohnson66 5y agoWhat specified behavior?
- josefx 5y agoPosix apparently explicitly allows calling programs with an empty argv, so it isn't just a Linux implementation detail Polkit failed to handle.
- TheRealPomax 5y agoWhich is fine: https://news.ycombinator.com/item?id=30208963 https://news.ycombinator.com/item?id=30208963 is pretty on the money here. Patch this behaviour, and fix the extremely low number of offending applications concurrently.
- kardos 5y agoThe sysctl can have three settings: 0 to do nothing, 1 to emit a warning, 2 to fully enable the patch that blocks argc=0. Use 1 by default as not to break userspace, let people opt-in to 2 for the additional security
- netr0ute 5y agoHow did polkit even fall victim to this? I sometimes feel hesitant to include an argv[0] in my execvX functions because I know that it's only "a convention" and not an enforced rule, and no reasonable program should expect it to be there.
- josephcsible 5y agoI wrote a summary here: https://news.ycombinator.com/item?id=30091667 https://news.ycombinator.com/item?id=30091667 The tl;dr is that pkexec writes to argv, and envp comes right after argv on the stack, so a missing check caused it to accidentally write to envp when argc == 0.
- josephcsible 5y agoAnything the kernel does to try to mitigate this would break userspace. It's one thing to do that when necessary to fix a kernel vulnerability, but I don't like the idea of breaking userspace just to try to work around some buggy userspace programs.
- 10000truths 5y agoYeah, I’m surprised that this patch is getting traction at all. If it’s a bug in the application, it should be fixed in the application. The kernel shouldn’t be changing the kernel-userspace interface to work around userspace bugs. There are plenty of bespoke binaries out there with no/lost source code that won’t get the benefit of scrutiny from the kernel dev team. I certainly wouldn’t assume that none of them invoke execve with a NULL or 0-item arg parameter, and merging this patch would break them without recourse.
- jandrese 5y agoGiven that this behavior is already disallowed (and faulting) on BSD and at least some commercial Unix flavors there shouldn't be too much software out there that relies on it. I would lean towards making it an API error BSD style and taking breakage on a case by case basis. I'm not the person who would be called up however, so it's easy for me to say. Worst case you can create a sysctl variable to enable/disable the check for those corner cases where it can't be fixed and other precautions can be taken against the security vulnerability.
- josephcsible 5y ago> Given that this behavior is already disallowed (and faulting) on BSD and at least some commercial Unix flavors there shouldn't be too much software out there that relies on it. How much commercial software targets UNIX these days?
- pjmlp 5y agoEnough to keep sales of AIX, Solaris, mainframes and several POSIX based RTOS going. And that little one named macOS as well.
- tremon 5y agoI don't really understand the rationale for changing the Linux kernel at all here; yes, this is yet another footgun (tm), but this is purely a userspace problem. Especially given the examples regarding backwards compatibility given in the comments, I would expect a long-lasting userspace fix to come in the form of a new, better libc API. Not by prohibiting certain behaviour from the kernel-side of things. That said, why is the obvious fix not simply "always populate argv[0] with pathname if called with NULL argv"?
- deleted 5y ago[deleted]
- MatmaRex 5y agoYour question is answered in the article: > Given the code we've found that depends on NULL argv, I think we likely can't make the change outright, so we're down this weird rabbit hole of trying to reject what we can and create work-around behaviors for the cases that currently exist.
- tremon 5y agoIt's not clear from that quote if they're talking about code calling execve(,NULL,) or a program expecting main() to be entered with argv[0]==NULL. (edit: found an answer here: https://lwn.net/ml/linux-kernel/5e963fab-88d4-2039-1cf4-6661e9bd16b@dereferenced.org/ https://lwn.net/ml/linux-kernel/5e963fab-88d4-2039-1cf4-6661...) multicall programs will try to use argv[0] and might crash in this scenario. If we're going to fake an argv, I guess we should try to do it right.
- formerly_proven 5y ago(multicall = things like busybox which look at argv[0] to determine what program they should act like)
- Animats 5y agoRight, backwards compatibility with existing bugs. It really isn't a kernel problem at all, of course. The POSIX spec says "The first argument, argv[0], is required and must contain the name of the executable file for the new process image."[1] Linux is not fully POSIX-compliant, but it generally follows the POSIX standard for kernel calls defined in POSIX. FreeBSD's man page says "At least one argument must be present in the array; by custom, the first element should be the name of the executed program (for example, the last component of path)."[2] So, "must", compatible with the POSIX spec. QNX implements the POSIX API, pretty much per the spec. The QNX spec says: "The value in argv[0] must point to a filename that's associated with the process being started."[4] So, "must", again compatible with the spec. The Linux man page says, unfortunately, "By convention, the first of these strings (i.e., argv[0]) should contain the filename associated with the file being executed."[3] That's the problem. POSIX says "must" but the Linux page only says "should". That was a mistake. One somewhat kludgy fix would be to disallow argv[0] == NULL && envp[0] != NULL. That allows launching a program with no args at all, which might be used somewhere in some ancient program, while disallowing launch with no command line args but with environment args, which is probably a bug. (I liked the QNX approach to program loading. The kernel isn't involved. The "exec" functions link to a shared object, which requests memory and does the loading using regular I/O operations. The kernel contains no loader and is thus immune to problems from malformed executables.) [1] https://support.sas.com/documentation/onlinedoc/sasc/doc750/html/lr2/zid-7281.htm https://support.sas.com/documentation/onlinedoc/sasc/doc750/... [2] https://manpages.debian.org/unstable/freebsd-manpages/execve.2freebsd.en.html https://manpages.debian.org/unstable/freebsd-manpages/execve... [3] https://www.man7.org/linux/man-pages/man2/execve.2.html https://www.man7.org/linux/man-pages/man2/execve.2.html [4] https://www.qnx.com/developers/docs/6.4.0/neutrino/lib_ref/e/execve.html https://www.qnx.com/developers/docs/6.4.0/neutrino/lib_ref/e...
- bombcar 5y agoInstead of fixing this in the kernel, couldn't this be added to SELinux as something it keeps an eye out for? The kernel could log it, and hopefully track them down, but breaking the ABI seems harsh.
- staticassertion 5y agoMaybe, but not necessarily. If a program's job is "elevate other program's privileges arbitrarily" your policy is gonna be pretty loose. Also, a lot of distros don't run SELinux. It's not to say that SELinux wouldn't be helpful, it's just that ideally we'd not have these vulnerabilities to begin with.
- mortenlarsen 5y agoOpenBSD has handled this case for some years. I do not know if there was any breakage or fallout from this. https://github.com/openbsd/src/commit/74212563870067f5b1e271876e1ec5a2fdf2f2e0 https://github.com/openbsd/src/commit/74212563870067f5b1e271...
- simcop2387 5y agoThat's not a bad precedent for saying it shouldn't break much of anything if it's done. I also love the name of the label for handling the error condition there.
- cozzyd 5y agoSlightly funnier would be "harmful"
- sedatk 5y agoFWIW, Windows (MSVC) also guarantees "argc > 0" and non-empty argv with non-empty argv[0]. It also returns EINVAL if execve validation fails.
- garaetjjte 5y agoWindows doesn't have argv at all, it uses single string as cmdline arguments.
- sedatk 5y agoThe C runtime bridges that gap. https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-170#standard-command-line-arguments https://docs.microsoft.com/en-us/cpp/cpp/main-function-comma...
- asveikau 5y agoAt the kernel level, yes. But they are talking about CRT, which parses the command line and makes argv from user space, if your linker is set up to have a main() and not WinMain. That is why they put MSVC in parens.
- amelius 5y agoI wish the kernel could handle argc==ARBITRARY_NUMBER It's crazy that with 64GB of internal memory, I'm still getting "argument list too long".
- saagarjha 5y agoArguments go on the stack, which has a fixed size.
- josephcsible 5y agohttps://lore.kernel.org/lkml/20170707185729.GA70967@beast/T/#m1a27b742141be749be06f276730756642b90d635 https://lore.kernel.org/lkml/20170707185729.GA70967@beast/T/... (merged as commit da029c1) added a hard upper limit of 6MB for argv + envp combined, no matter how big the process's stack is.
- dmatech 5y agoIt seems abundantly clear that programs are allowed to pass an empty list as argv to execve, although these programs might not be POSIX-compliant. As for the "main" function, argc is only guaranteed to be nonnegative. A well-written program therefore cannot assume that argc is not zero. Additionally, there is no guarantee that a POSIX-compliant program will be called by another POSIX-compliant program. So it looks like pkexec made the mistake here by not carefully adhering to the standard. One can argue that argc==0 is a bad idea, and it is. But it's technically legal, and changing this would break userspace programs. 1. https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html https://pubs.opengroup.org/onlinepubs/9699919799/functions/e... 2. https://eel.is/c++draft/basic.start.main https://eel.is/c++draft/basic.start.main
- tptacek 5y agoObviously Linux programs can pass an empty list to argv, since that's how we got the pkexec vulnerability.
- stefan_ 5y agoargc = 0 is a bad idea? When did we enter into this insane parallel universe? When I first came across (argc, argv) a long long time ago I was befuddled by the choice of having argv[0] be something vaguely related to your binary, maybe the path to it. That seemed a very weird choice seeing how most programs have no use for that and now all the arguments you actually care about are shifted. So now you have a patch to ensure argc is always 1. Only of course if you accept that there are programs that misbehave with argc=0, surely you must also accept there are programs that misbehave if argv[0] is not the program path? This is after all the convention that got you into this misery in the first place! That's the moment the stupidity fully sets in. So now you write a patch to make sure argv[0] is really the path! Except just as we have discovered with this innocuous patch, you could write a dissertation on the question of "what exactly is meant with the program path" alone. And that really whoever was relying on some specific interpretation on the value of argv[0] would have been much better off with some other syscall to tell him what he was after.
- Someone 5y ago
- ezekiel68 5y agoIt's amusing to me that some people seem to think it's possible to create "secure computing" with no footguns whatsoever. A surprising amount of the infrastructure upon which we write computer programs (including a kernel and its extended OS) relies upon "mere" conventions. Indeed one of the lowest levels of all is named the "calling convention". Software which breaks these conventions, yet somehow "runs" or functions to some extent before causing problems is broken software, period. It is not a cool hack or an interesting lead for further research. Any hand-wringing about it whatsoever is a waste of time. If some individuals scale the protective barrier and jump to their deaths in the Grand Canyon, this does not indicate any deficiency in the protective barrier. It was not (and should not) be designed to prevent such misuse.
- tptacek 5y agoIt's weird to have strong opinions about userspace here given that the code search Kees Cook provided includes only test cases (and, amusingly, what looks like one piece of shellcode) --- the followup search on `execlp` turned up some things that looked like they might be non-test hits, but it turned out that only the test case hits were argc==0. There's also comments throughout the thread that POSIX requires programs to accept argc==0, which does not appear to be the case --- the opposite seems closer to the truth, in that "strictly conforming" POSIX programs must have argc>0. Not breaking userspace is an important goal, but breaking bad test cases doesn't meaningfully break userland. The flip side of this is that people went looking for additional instances of the pkexec bug and none have, as far as I can see, turned up yet.
- tedunangst 5y ago"But what if it breaks userland" does seem to be something of a fig leaf. Change would imply there may have been something wrong with the previous code.
- stormbrew 5y agoYes this. This really feels like a vast over-extension of the "don't break userland" axiom. It's obvious with only a little thought that many bugs in a kernel can be accidentally relied on somehow, and many almost certainly have been and have still been fixed in the linux kernel anyways (maybe by accident). It seems like a Linus quote from one of his notorious angry emails turned into a meme that got out of hand and now people are taking it too seriously. And yeah, the posix quotes that have been presented paint a picture much more like "posix doesn't demand you not support null/empty argv or argc==0, but it would prefer you not" to me.
- mst 5y agoGiven "BSD systems already prohibit an empty argv" it certainly seems unlikely that it's a standards related issue in practice. So (as they appear to be doing if I read correctly) keeping the patch in the tree for the moment, seeing what if anything actually breaks in practice, and then trying to figure out what the best answer is from there, is probably the only thing to do. It's always a horrible day for a developer when they discover they have a bugwards compatibility problem and while it's, I guess, debatable if that's exactly what we're seeing here, it's close enough to the pain I know well that I have much sympathy for everybody involved here.
- kortex 5y agoSo let me paraphrase to see if I understand the context correctly: - the only guaranteeS are argc>=0, argv[argc] == NULL, and envp comes right after argv - argc is _almost always_ >= 1 - a widely used sbin (polkit) just assumes argv is always at least 1 element and so iterates starting at argv[1] (without checking argc) - said polkit also mutates argv, so if it is fed with argc actually zero, it actually mutates envp - some devs want a patch to the kernel to prevent calling execve with argc==0, (which probably should have been in the original spec, but that ship has sailed) as a way of preventing other similar footguns. - other devs are like "that's gonna potentially break userspace" which is a valid concerne Is that more or less it?
- johnny22 5y agowasn't it pkexec specifically, not polkit generally that was the problem?
- deleted 5y ago[deleted]
- kortex 5y agoYeah that sounds more technically correct.
- AdamJacobMuller 5y agoaside from "that ship has sailed" yes. The debate seems to be if they can actually do it safely or not, and from the article it seems like they think they can actually do it safely.
- kortex 5y ago"That ship has sailed" i.e. POSIX forbidding argc==0 when standardizing. I agree it's worth considering it now. I think argc>0 is close enough to a convention that things ought to mostly work, but it kind of irks me that it's necessary. But it should reduce ambiguity.
- MrStonedOne 5y ago
- eriksjolund 5y agoA few years ago I wrote a small program and script that search for executables that crash when they are executed with argc==0 https://github.com/eriksjolund/empty-argv-segfault-check https://github.com/eriksjolund/empty-argv-segfault-check
- charcircuit 5y agoargc has always been able to be 0 and I've always handled that case It's not the kernels job to change this just because some developers don't understand it may be 0.
- Matheus28 5y agoAm I crazy or isn’t the simplest solution being the kernel filling in argv[0] with the executable path when argc is 0 (thus changing it to 1)? What would be the downside in that?
- cryptonector 5y ago> As noted in some of the test cases, there are comments like > "Solaris doesn't support this," etc. Evidence that NULL argv should be removed. Also, at the very least do it for execs of set-uid executables.