Live data from Hacker News

Why bother with argv[0]?

wietzebeukema.nl

251–260 of 277 posts

Re: Why bother with argv[0]?

#251

So obviously claiming that there's no good reason for process to read argv[0] is either demonstrating the author's ignorance or needs a much stronger defense; I'd be fascinated to hear how they think busybox should work on an OpenWrt box with a 16MB root filesystem. However , I am willing to consider the discussion about whether there could be merit to restricting the ability to write that value; I could imagine a sy…

Yeah, never mind shutdown/reboot, has the author heard of busybox?

Re: Why bother with argv[0]?

#252
post #230
post #229

Earlier quoted context omitted.

[flagged]

> it's against the rules here to those kinds of fake quotes. What part of the guidelines are you referring to? Also, despite the quotation marks, I don't think they mean to quote you. They're just rephrasing you as they understood you. Coincidentally enough, I've just done that too in another comment: https://news.ycombinator.com/item?id=41442007

https://news.ycombinator.com/context?id=13602947>

And I didn't mention the guidelines (i.e. newsguidelines.html). On that note, though:

> the site guidelines[...] aren't a list of proscribed behaviors but a set of values to internalize. I'd say "Please respond to the strongest plausible interpretation of what someone says, not a weaker one that's easier to criticize" covers this case pretty squarely

(from https://news.ycombinator.com/item?id=15892014#15893789>)

See also:

https://news.ycombinator.com/item?id=38688831#38690517>

plus lots (and lots) more.

Re: Why bother with argv[0]?

#253

Earlier quoted context omitted.

is being able to run busybox part of that specification? Linux ELF files are tagged for Linux OS, not generic Unix OS.

> is being able to run busybox part of that specification? It's the other way round. This is like asking if running RHEL is part of the specification? Obviously not. But RHEL provides an environment that is quite close to the specification. So is running a busybox part of the specification? Obviously not. But Busybox provides an environment that is close to the speficiation.

Then it would be busybox's responsibility to make busybox work.

Re: Why bother with argv[0]?

#254
> and (especially a few decades ago) can offer cross-platform/backwards syntax compatibility using a shared code base.

This is still very much an issue. For the shutdown and reboot case, the main reason those symlinks is exist is for backwards compatibility for existing programs and scripts (and muscle memory) that assume there is a shutdown or reboot command, and compatibility with systems that don't use systemd.

Another way to do that could be to use a shell script that execs systemctl, but that requires a separate intermediate shell process, which may have its own compatibility issues.

Another use of argv[0] that isn't discussed at all is putting a hyphen at the beginning of argv[0] for login shells. For example if bash is invoked as the login shell argv[0] is "-bash". That probably wasn't a great design decision, but changing it now would probably cause a lot of breakage.

Re: Why bother with argv[0]?

#255
post #206

Earlier quoted context omitted.

Hmm... I generally have so many issues with chdir (e.g. someone gives you a relative path to a file you need to read and now that's screwed up because you did a previous chdir) that I just avoid all use of it in the first place. Generally don't run into chroot all that often these days & docker gives you a fully virtualized environment where if a relative path is garbage then you may have other problems too (e.g. giv…

It's very common for daemons/servers to chdir("/") at the top of main. Relative paths sent by clients getting broken is a feature, not a bug. (In fact I just fixed a bug related to this an hour ago because a relative path was not being canonicalized before being passed to the daemon I'm working on and it caused a file to be written to the wrong place). There's no way create a process such that /proc/self/exe is incor…

> It's very common for daemons/servers to chdir("/") at the top of main. Relative paths sent by clients getting broken is a feature, not a bug.

I instead put that in the lauhch script / systemd policy. That way when I run the server locally for development weird shit doesn’t happen in my root.

Re: Why bother with argv[0]?

#256

Earlier quoted context omitted.

I don't think argv[0] includes the full path (or at least some programming language strip the whole path and keep only the last part)

I stand corrected - C, Go and Python are all consistent here and show the full path. I seem to recall there was a language that only provided the stripped part - but I guess my memory is failing me here. Sorry for the wrong information above.

Languages might remove the dirname part, but argv[0] is not necessarily a path, it's just a string passed to the exec system call that is also passed to main(int argc, char *argv). While many languges don't call their main function that, somewhere in the runtime that's what it gets.

Re: Why bother with argv[0]?

#257
> This seems like a questionable design decision.

Nope.

> Should a program be allowed to behave differently based on its name?

Yes. The program can also inspect any other part of its environment, including the parent process. What makes sense to inspect here depends on the particular program in question. The symlink example is still useful today.

> From a 2020s standpoint, this seems highly undesirable

Nope.

> it makes software less predictable

It doesn't. It makes it more predictable if programs can easily provide compatibility interfaces. Yes, you could do the same with a wrapper but removing friction matters.

> and goes against modern design principles.

Then modern design priciples can take a hike.

> Today however, disk space is no longer considered an issue

It should be considered an issue though. I buy better hardware to get more use out of it, not for lazy developers to needlessly piss it all away.

This is just yet nother example of "securit" people trying to make their lifes easier by making other's lifes harder. And as usual it's only theater since almost all of the "exploits" apply to arguments as well which for many programs provide plenty opportunity to include arbitrary strings. Fix your tools instead of expecting the world to work around their limitations.

Re: Why bother with argv[0]?

#258
post #247

Earlier quoted context omitted.

The real security flaw is extracting a value from a process's own memory to identify what the process is. If you want a secure way to identify what a process is and where it came from, that needs to be a new feature in the OS. argv[0] was designed to be part of the arguments to the program, and it succeeds perfectly at that task. The problem is that it has been abused by external tools as a way to identify the progra…

> it has been abused by external tools as a way to identify the program just because there was no other alternative. There is an alternative, at least on linux: /proc/$pid/exe. And if your question is "what executable is running" that is a better way to get it. But for a program like busybox, argv[0] is also important.

Pretty much any OS lets you examine which binaries have been mapped into the process adddress space meaning there are plenty alternatives.

Re: Why bother with argv[0]?

#259

Earlier quoted context omitted.

That seems unnecessarily harsh. I don't think that's the gist of the article, but the throwaway suggestion of 'just make lots of copies, who cares about diskspace' is insufficient and thus distracts. It's.. a single line about solutions in an article that isn't _about_ solving problems, it's about highlighting a problem exists and that it's worth solving. I read the article more as: There is __often__ no good reason…

There's the `setproctitle` in FreeBSD that is designed exactly for a process to update the information that is presented to tools such as ps. https://man.freebsd.org/cgi/man.cgi?query=setproctitle&aprop...

prctl(PR_SET_NAME) on Linux - sets the thread name, and the name of the main thread is shown as the process name in most tools.

Re: Why bother with argv[0]?

#260

So obviously claiming that there's no good reason for process to read argv[0] is either demonstrating the author's ignorance or needs a much stronger defense; I'd be fascinated to hear how they think busybox should work on an OpenWrt box with a 16MB root filesystem. However , I am willing to consider the discussion about whether there could be merit to restricting the ability to write that value; I could imagine a sy…

Not an author, but there's a good alternative. If busybox was edited to ignore argv[2], then applets could be called via shebangs, instead of symlinks: $ echo '#!/path/to/busybox echo' > myecho $ chmod +x myecho $ ./myecho 123 ./myecho 123 Right now this doesn't work properly, because "./myecho" (argv[0]) gets placed into argv[2] of the process. Otherwise, this technique IMHO is better than symlinks: - Each applet us…

Are shebangs recursive? Otherwise this means that busybox can no longer provide /bin/sh.
Post reply on HN