Skip to content

Do not modify PATH when preparing services - #1087

Open
stacyharper wants to merge 1 commit into
OpenRC:masterfrom
stacyharper:wbarraco/xzlszllxynxu
Open

stacyharper wants to merge 1 commit into
OpenRC:masterfrom
stacyharper:wbarraco/xzlszllxynxu

Conversation

@stacyharper

@stacyharper stacyharper commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Currently we re-order and add missing parts to the PATH. We do so for
security reason, to prevent openrc to use binaries that are not
installed on system locations. We also prefix the LIBEXECDIR to make
the binaries available to scripts.

Doing so modify the PATH, making services to run with an unexpected
value. This can break openrc-user services.

To solve this, instead of re-ordering and adding missing parts, we just
prefix the value with RC_PATH_PREFIX in misc.c. Then on functions.sh,
we drop all PATH additional manipulations, and we strip this whole
prefix on start-stop-daemon, and supervise-daemon.

it is an alternative to: #1083

@stacyharper stacyharper changed the title WIP:Overhaul how we deal with PATH WIP: Overhaul how we deal with PATH Sep 26, 2026
Comment thread src/shared/misc.c Outdated
if ((path = getenv("PATH"))) {
xasprintf(&p, "%s:%s", RC_PATH_PREFIX, path);
} else {
xasprintf(&p, "%s:%s", RC_PATH_PREFIX, RC_PATH_DEFAULT);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this ends up creating a path that has /bin /sbin /usr/bin /usr/sbin twice

would be better to fixup RC_PATH_PREFIX to be our default value asw

@stacyharper stacyharper Sep 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is expected, because we will strip the prefix, and only the default value will remains. We could make the prefix the default, but then we have to detect this situation, and then not trim the prefix. And also this implies that the prefix become the default, with the local parts, and parts included in a correct order. Am not sure if this way is more simple

Comment thread src/shared/misc.c Outdated
} else {
xasprintf(&p, "%s:%s", RC_PATH_PREFIX, RC_PATH_DEFAULT);
}
setenv("PATH", p, 1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xasprintf(&p, "PATH=%s", RC_PATH_PREFIX);
putenv(p);

no need to double allocate (once here, once by libc inside setenv)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah right! thanks I was unaware that that string used with putenv become part of the env.

Comment on lines +1064 to +1074
len = strlen(RC_PATH_PREFIX ":");
if (strncmp(token, RC_PATH_PREFIX ":", len) == 0) {
newpath = xmalloc(strlen(token) - len + 1);
strcpy(newpath, token + len);
} else {
newpath = xmalloc(strlen(token) + 1);
strcpy(newpath, token);
}
*np = '\0';
unsetenv("PATH");
setenv("PATH", newpath, 1);
free(newpath);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to allocate memory here, since we're no longer editing the path

if (matches_prefix)
    setenv("PATH", &token[len], true);

(and nothing to be done if token doesn't match, it is already the value of PATH, ditto for s-d)

@stacyharper stacyharper Sep 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, dunno why I made this that complicated

@stacyharper stacyharper changed the title WIP: Overhaul how we deal with PATH Simplify how we deal with PATH Sep 27, 2026
@stacyharper stacyharper changed the title Simplify how we deal with PATH Do not modify PATH when preparing services Sep 27, 2026
Currently we re-order and add missing parts to the PATH. We do so for
security reason, to prevent openrc to use binaries that are not
installed on system locations. We also prefix the LIBEXECDIR to make
the binaries available to scripts.

Doing so modify the PATH, making services to run with an unexpected
value. This can break openrc-user services.

To solve this, instead of re-ordering and adding missing parts, we just
prefix the value with RC_PATH_PREFIX in misc.c. Then on functions.sh,
we drop all PATH additional manipulations, and we strip this whole
prefix on start-stop-daemon, and supervise-daemon.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants