Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > muc.lists.netbsd.tech.userlevel > #11967 > unrolled thread

Re: Proposal: getexecpath(3)

Started byBrad Smith <brad@comstyle.com>
First post2026-10-25 04:20 -0400
Last post2026-11-01 19:34 +0000
Articles 7 — 5 participants

Back to article view | Back to muc.lists.netbsd.tech.userlevel


Contents

  Re: Proposal: getexecpath(3) Brad Smith <brad@comstyle.com> - 2026-10-25 04:20 -0400
    Re: Proposal: getexecpath(3) Taylor R Campbell <riastradh@NetBSD.org> - 2026-10-26 07:29 +0000
      Re: Proposal: getexecpath(3) Mouse <mouse@Rodents-Montreal.ORG> - 2026-10-26 07:04 -0400
        Re: Proposal: getexecpath(3) Taylor R Campbell <riastradh@NetBSD.org> - 2026-10-26 12:17 +0000
          Re: Proposal: getexecpath(3) Mouse <mouse@Rodents-Montreal.ORG> - 2026-10-26 19:12 -0400
            Re: Proposal: getexecpath(3) Jason Thorpe <thorpej@me.com> - 2026-10-27 08:59 -0400
          Re: Proposal: getexecpath(3) David Holland <dholland-tech@netbsd.org> - 2026-11-01 19:34 +0000

#11967 — Re: Proposal: getexecpath(3)

FromBrad Smith <brad@comstyle.com>
Date2026-10-25 04:20 -0400
SubjectRe: Proposal: getexecpath(3)
Message-ID<210e9bde-0c69-4db4-9b68-6a78bf3ad615@comstyle.com>
On Mon, Dec 16, 2024, Taylor R Campbell wrote:
 > PROPOSAL: New libc function
 >
 >     const char *getexecpath(void);
 >
 > returns the pathname that was passed to execve(2), unmodified.

I know this is not quite what you are looking for, but FYI, OpenBSD
has since added a getexecpath(3) to libc, first appearing in
OpenBSD 8.0:

   https://man.openbsd.org/getexecpath.3

Unlike NetBSD, OpenBSD did not even have a sysctl like
KERN_PROC_PATHNAME to fall back on, so software was left guessing
from argv[0]. That said, after looking through many open source
projects, I see a lot of them have not been updated to use NetBSD's
sysctl either.

   #include <unistd.h>

   int
   getexecpath(char *buf, size_t bufsize);

It copies the absolute pathname of the executable into buf,
canonicalized the same way as realpath(3), and returns 0 on success
or -1 with errno set on failure (e.g. the executable was started in
a directory no longer linked into the filesystem, or the buffer is
too small; PATH_MAX is the recommended size).

The path is computed by the kernel in execve(2) and placed on the
new process's stack as an ELF auxiliary vector entry, which libc
picks up at startup. The relevant commits:

https://github.com/openbsd/src/commit/4dde6567f895c6faa36acbacd4cdf62954a70399
     In execve(2), attempt to create a realpath buffer for the
     executable and pass it to userland as an auxval

https://github.com/openbsd/src/commit/026846f111232f45cd1f4883414c190c8ff722a7
     Add getexecpath(3) to libc

https://github.com/openbsd/src/commit/1d46e5b94d0221c062218de40c6d15f9f5cacb31
     Make getexecpath() return the ELF executable that is running
     (for a script run via a shebang line, the canonicalized path of
     the interpreter actually executed)

So the name is the same, but both the signature and the semantics
differ from what is proposed here: a canonical absolute path rather
than the verbatim execve(2) argument. If NetBSD goes ahead with
this, it would be good to either match OpenBSD's interface or pick a
different name. Portable software is already starting to probe for
getexecpath() (HAVE_GETEXECPATH checks in configure/meson/CMake), and
two incompatible functions under the same name would be an
unpleasant trap.

On the consumer side, I have been adding getexecpath() support for
OpenBSD to quite a few common open source packages, Racket among
them, which uses the same kind of self-path logic that tripped up
lang/racket in your postscript. A compatible interface on NetBSD
would let the same code paths be reused.


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de

[toc] | [next] | [standalone]


#11973

FromTaylor R Campbell <riastradh@NetBSD.org>
Date2026-10-26 07:29 +0000
Message-ID<20261026072940.AC5DF84D2D@mail.netbsd.org>
In reply to#11967
> Date: Sun, 25 Oct 2026 04:20:57 -0400
> From: Brad Smith <brad@comstyle.com>
> 
> So the name is the same, but both the signature and the semantics
> differ from what is proposed here: a canonical absolute path rather
> than the verbatim execve(2) argument. If NetBSD goes ahead with
> this, it would be good to either match OpenBSD's interface or pick a
> different name.

Why do you want the kernel to compute some notion of a canonical
absolute path?  Of what use is this for applications?  Isn't this
strictly less useful than the original verbatim exec path?

You can always pass the original verbatim exec path to realpath(3) if
you want to canonicalize it, but you can't undo realpath(3) (or the
kernel's equivalent) if the program wants to use the same symlinks for
to (dl)open supporting data and libraries as the caller used to
execute the program.

I think it is a mistake for the kernel to expose anything other than
the original verbatim exec path.

(I don't have strong feelings about char *getexecpath(void) vs void
getexecpath(char *, size_t); the first one is a little simpler, but
whatever.)

> Portable software is already starting to probe for getexecpath()
> (HAVE_GETEXECPATH checks in configure/meson/CMake), and two
> incompatible functions under the same name would be an unpleasant
> trap.

Sounds like the `portable' software needs #ifdef __OpenBSD__, since
API this isn't portable to any platform except OpenBSD anyway?

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de

[toc] | [prev] | [next] | [standalone]


#11974

FromMouse <mouse@Rodents-Montreal.ORG>
Date2026-10-26 07:04 -0400
Message-ID<202610261104.HAA21730@Stone.Rodents-Montreal.ORG>
In reply to#11973
> Why do you want the kernel to compute some notion of a canonical
> absolute path?  Of what use is this for applications?  Isn't this
> strictly less useful than the original verbatim exec path?

It also can leak information.  If the path passed to execve is relative
and one of the containing directories is inaccessible to the process, a
kernel-created absolute path will leak a pathname the program shouldn't
be able to obtain.

> You can always pass the original verbatim exec path to realpath(3) if
> you want to canonicalize it,

True, though that - at best - returns the hard path at the time of
realpath (or more precisely with pieces fetched at various times
scattered throughout the execution of realpath), which may not be the
same as the one at the time of exec.

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse@rodents-montreal.org
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de

[toc] | [prev] | [next] | [standalone]


#11975

FromTaylor R Campbell <riastradh@NetBSD.org>
Date2026-10-26 12:17 +0000
Message-ID<20261026121701.83CF784D39@mail.netbsd.org>
In reply to#11974
> Date: Mon, 26 Oct 2026 07:04:08 -0400 (EDT)
> From: Mouse <mouse@Rodents-Montreal.ORG>
> 
> > You can always pass the original verbatim exec path to realpath(3) if
> > you want to canonicalize it,
> 
> True, though that - at best - returns the hard path at the time of
> realpath (or more precisely with pieces fetched at various times
> scattered throughout the execution of realpath), which may not be the
> same as the one at the time of exec.

If the path resolution may have concurrently changed between the
execve call and the realpath call, it may as well have concurrently
changed between the execve call and the open call.

In other words, this race condition is inherent to the nature of
passing a path around; it cannot be avoided by asking the kernel to do
(the equivalent of) realpath.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de

[toc] | [prev] | [next] | [standalone]


#11976

FromMouse <mouse@Rodents-Montreal.ORG>
Date2026-10-26 19:12 -0400
Message-ID<202610262312.TAA09309@Stone.Rodents-Montreal.ORG>
In reply to#11975
>>> You can always pass the original verbatim exec path to realpath(3)
>>> if you want to canonicalize it,
>> True, though that - at best - returns the hard path at the time of
>> realpath (or more precisely with pieces fetched at various times
>> scattered throughout the execution of realpath), which may not be
>> the same as the one at the time of exec.
> If the path resolution may have concurrently changed between the
> execve call and the realpath call, it may as well have concurrently
> changed between the execve call and the open call.

Good point.  Since (at least as far as I recall) we don't know why
anyone (putatively) wants the original path, we don't know whether this
matters.  But for most likely purposes I would expect it would.

Maybe what this wants is a way to flag the executable such that it
automatically gets something like a descriptor open on the directory
containing the executable?  I'm not sure that's quite the right
semantic, but maybe it's close enough to be a useful starting point.

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse@rodents-montreal.org
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de

[toc] | [prev] | [next] | [standalone]


#11977

FromJason Thorpe <thorpej@me.com>
Date2026-10-27 08:59 -0400
Message-ID<8AE2EC29-A8BC-49D4-950D-53D441F3DB29@me.com>
In reply to#11976
> On Oct 26, 2026, at 7:12 PM, Mouse <mouse@rodents-montreal.org> wrote:
> 
> Good point.  Since (at least as far as I recall) we don't know why
> anyone (putatively) wants the original path, we don't know whether this
> matters.  But for most likely purposes I would expect it would.

It’s useful for having “relocatable” (in the file system) applications, so that a program could find its resources without having to bake in path prefixes as compile time.

-- thorpej


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de

[toc] | [prev] | [next] | [standalone]


#11999

FromDavid Holland <dholland-tech@netbsd.org>
Date2026-11-01 19:34 +0000
Message-ID<aueUVN7AxY-phkJO@netbsd.org>
In reply to#11975
On Mon, Oct 26, 2026 at 12:17:00PM +0000, Taylor R Campbell wrote:
 > > > You can always pass the original verbatim exec path to realpath(3) if
 > > > you want to canonicalize it,
 > > 
 > > True, though that - at best - returns the hard path at the time of
 > > realpath (or more precisely with pieces fetched at various times
 > > scattered throughout the execution of realpath), which may not be the
 > > same as the one at the time of exec.
 > 
 > If the path resolution may have concurrently changed between the
 > execve call and the realpath call, it may as well have concurrently
 > changed between the execve call and the open call.
 > 
 > In other words, this race condition is inherent to the nature of
 > passing a path around; it cannot be avoided by asking the kernel to do
 > (the equivalent of) realpath.

If the realpathing is done as part of namei, then there's only one
lookup sequence and the resulting realpath points to the same file as
the program gets loaded from, as of the time the lookup in exec is
completed. Mostly. If it's an absolute path. Relative paths create
further issues.

Nothing prevents elements of that path from changing later before it's
looked up again. But I suspect the intent is to canonicalize it into
something the invoking user has no write access to.

Note that our namei does this and what gets dumped into the ELF
auxiliary vector has been handled this way. I don't know if OpenBSD
merged my code or reimplemented it (and if they reimplemented it,
whether they got it right) but that's their problem :-)

-- 
David A. Holland
dholland@netbsd.org

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de

[toc] | [prev] | [standalone]


Back to top | Article view | muc.lists.netbsd.tech.userlevel


csiph-web