pseudo: do not expand symlinks in /proc
PATCH 1/2 pseudo: do not expand symlinks in /proc
Some symlinks in /proc, such as those under /proc/[pid]/fd, /proc/[pid]/cwd, and /proc/[pid]/exe that are not real and should not have readlink called on them. These look like symlinks, but behave like hardlinks. Readlink does not return actual paths. Previously pseudo_fix_path would expand files such as /dev/stdin to paths such as /proc/6680/fd/pipe:[1270830076] which do not exist.
This issue affects:
- deleted files
- deleted directories
- fifos
- sockets
- anon_inodes (epoll, eventfd, inotify, signalfd, timerfd, etc)
Testing:
timed builds before and after applying patch, without significant measurable difference.
$ bitbake -c compile <image>; time bitbake <image>
installed pseudo on an image and was unable to reproduce the test on bug report after applying the patch.
[YOCTO #13288]
--- meta/recipes-devtools/pseudo/files/proc.patch | 86 +++++++++++++++++++ meta/recipes-devtools/pseudo/pseudo_git.bb | 1 + 2 files changed, 87 insertions(+) create mode 100644 meta/recipes-devtools/pseudo/files/proc.patch
meta/recipes-devtools/pseudo/files/proc.patch new file +86 -0
@@ -0,0 +1,86 @@ +From: Matt Cowell <[email protected]> +From: "Sakib Sajal" <[email protected]> + +pseudo: do not expand symlinks in /proc + +Some symlinks in /proc, such as those under /proc/[pid]/fd, +/proc/[pid]/cwd, and /proc/[pid]/exe that are not real and should not +have readlink called on them. These look like symlinks, but behave like +hardlinks. Readlink does not return actual paths. Previously +pseudo_fix_path would expand files such as /dev/stdin to paths such as +/proc/6680/fd/pipe:[1270830076] which do not exist. + +This issue affects: +- deleted files +- deleted directories +- fifos +- sockets +- anon_inodes (epoll, eventfd, inotify, signalfd, timerfd, etc) + +Testing: +timed builds before and after applying patch, without significant +measurable difference. +$ bitbake -c compile <image>; time bitbake <image> + +installed pseudo on an image and was unable to reproduce the test +on bug report after applying the patch. + +[YOCTO #13288] + +Signed-off-by: Sakib Sajal <[email protected]> +--- + pseudo_util.c | 27 +++++++++++++++++++++++++++ + 1 file changed, 27 insertions(+) + +diff --git a/pseudo_util.c b/pseudo_util.c +index c867ed6..bce4d1e 100644 +--- a/pseudo_util.c ++++ b/pseudo_util.c +@@ -21,6 +21,8 @@ + #include <sys/time.h> + #include <unistd.h> + #include <limits.h> ++#include <sys/vfs.h> ++#include <linux/magic.h> + + /* see the comments below about (*real_regcomp)() */ + #include <dlfcn.h> +@@ -29,6 +31,11 @@ + #include "pseudo_ipc.h" + #include "pseudo_db.h" + ++/* O_PATH is defined in glibc 2.16 and later only */ ++#ifndef O_PATH ++#define O_PATH 010000000 ++#endif ++ + struct pseudo_variables { + char *key; + size_t key_len; +@@ -677,6 +684,26 @@ pseudo_append_element(char *newpath, char *root, size_t allocated, char **pcurre + */ + if (!leave_this && is_dir) { + int is_link = S_ISLNK(buf->st_mode); ++ ++ /* do not expand symlinks in the proc filesystem, since they may not be real */ ++ if (is_link) { ++ struct statfs sfs; ++ int fd; ++ ++ /* statfs follows symlinks, so use fstatfs */ ++ fd = open(newpath, O_CLOEXEC | O_PATH | O_NOFOLLOW); ++ if (-1 != fd) { ++ if (0 == fstatfs(fd, &sfs) && sfs.f_type == PROC_SUPER_MAGIC) { ++ pseudo_debug(PDBGF_PATH | PDBGF_VERBOSE, ++ "pae: '%s' is procfs symlink, not expanding\n", ++ newpath); ++ is_link = 0; ++ } ++ ++ close(fd); ++ } ++ } ++ + if (link_recursion >= PSEUDO_MAX_LINK_RECURSION && is_link) { + pseudo_diag("link recursion too deep, not expanding path '%s'.\n", newpath); + is_link = 0;
meta/recipes-devtools/pseudo/pseudo_git.bb +1 -0
@@ -9,6 +9,7 @@ SRC_URI = "git://git.yoctoproject.org/pseudo;branch=oe-core \ file://xattr_fix.patch \ file://mayunlink.patch \ file://pathfix.patch \ + file://proc.patch \ file://fallback-passwd \ file://fallback-group \ "
Signature · 1 line
2.25.1
PATCH 2/2 pseudo: Fix statx function usage
There is magic in the posts where specific variable names have specific magic. For that magic to work, "path" needs to be used not "pathname" as is currently there. Fix this, which fixes path issues on systems using statx (Ubuntu 20.04 in particular).
--- .../pseudo/files/statxfix.patch | 56 +++++++++++++++++++ meta/recipes-devtools/pseudo/pseudo_git.bb | 1 + 2 files changed, 57 insertions(+) create mode 100644 meta/recipes-devtools/pseudo/files/statxfix.patch
meta/recipes-devtools/pseudo/files/statxfix.patch new file +56 -0
@@ -0,0 +1,56 @@ +There is magic in the posts where specific variable names have specific +magic. For that magic to work, "path" needs to be used not "pathname" as +is currently there. Fix this, which fixes path issues on systems using +statx (Ubuntu 20.04 in particular). + +Signed-off-by: Richard Purdie <[email protected]> +Upstream-Status: Pending + +Index: git/ports/linux/statx/guts/statx.c +=================================================================== +--- git.orig/ports/linux/statx/guts/statx.c ++++ git/ports/linux/statx/guts/statx.c +@@ -5,14 +5,14 @@ + * SPDX-License-Identifier: LGPL-2.1-only + * + * int +- * statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf) { ++ * statx(int dirfd, const char *path, int flags, unsigned int mask, struct statx *statxbuf) { + * int rc = -1; + */ + pseudo_msg_t *msg; + PSEUDO_STATBUF buf; + int save_errno; + +- rc = real_statx(dirfd, pathname, flags, mask, statxbuf); ++ rc = real_statx(dirfd, path, flags, mask, statxbuf); + save_errno = errno; + if (rc == -1) { + return rc; +@@ -25,16 +25,16 @@ + buf.st_mode = statxbuf->stx_mode; + buf.st_rdev = makedev(statxbuf->stx_rdev_major, statxbuf->stx_rdev_minor); + buf.st_nlink = statxbuf->stx_nlink; +- msg = pseudo_client_op(OP_STAT, 0, -1, dirfd, pathname, &buf); ++ msg = pseudo_client_op(OP_STAT, 0, -1, dirfd, path, &buf); + if (msg && msg->result == RESULT_SUCCEED) { +- pseudo_debug(PDBGF_FILE, "statx(path %s), flags %o, stat rc %d, stat uid %o\n", pathname, flags, rc, statxbuf->stx_uid); ++ pseudo_debug(PDBGF_FILE, "statx(path %s), flags %o, stat rc %d, stat uid %o\n", path, flags, rc, statxbuf->stx_uid); + statxbuf->stx_uid = msg->uid; + statxbuf->stx_gid = msg->gid; + statxbuf->stx_mode = msg->mode; + statxbuf->stx_rdev_major = major(msg->rdev); + statxbuf->stx_rdev_minor = minor(msg->rdev); + } else { +- pseudo_debug(PDBGF_FILE, "statx(path %s) failed, flags %o, stat rc %d, stat uid %o\n", pathname, flags, rc, statxbuf->stx_uid); ++ pseudo_debug(PDBGF_FILE, "statx(path %s) failed, flags %o, stat rc %d, stat uid %o\n", path, flags, rc, statxbuf->stx_uid); + } + errno = save_errno; + /* return rc; +Index: git/ports/linux/statx/wrapfuncs.in +=================================================================== +--- git.orig/ports/linux/statx/wrapfuncs.in ++++ git/ports/linux/statx/wrapfuncs.in +@@ -1 +1 @@ +-int statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf); ++int statx(int dirfd, const char *path, int flags, unsigned int mask, struct statx *statxbuf);
meta/recipes-devtools/pseudo/pseudo_git.bb +1 -0
@@ -10,6 +10,7 @@ SRC_URI = "git://git.yoctoproject.org/pseudo;branch=oe-core \ file://mayunlink.patch \ file://pathfix.patch \ file://proc.patch \ + file://statxfix.patch \ file://fallback-passwd \ file://fallback-group \ "
Signature · 1 line
2.25.1
Re: [OE-core] [PATCH 2/2] pseudo: Fix statx function usage
Quoted 4 lines · On Wed, 7 Oct 2020 17:20:18 +0100 "Richard Purdie"…
+There is magic in the posts where specific variable names have specific +magic. For that magic to work, "path" needs to be used not "pathname" as +is currently there. Fix this, which fixes path issues on systems using +statx (Ubuntu 20.04 in particular).
So, usually if "pathname" is used, it's intentional to suppress the special magic behavior. Usually, but not always. In this case, the man page does use "pathname" and sometimes that means it's just a cut and paste error.
My man page also says there's no glibc wrapper for statx, making me slightly surprised that a wrapper for it is needed/relevant.
-s
Re: [OE-core] [PATCH 2/2] pseudo: Fix statx function usage
Quoted 8 lines · Seebs
Quoted 4 lines · On Wed, 7 Oct 2020 17:20:18 +0100 "Richard Purdie"…
+There is magic in the posts where specific variable names have specific +magic. For that magic to work, "path" needs to be used not "pathname" as +is currently there. Fix this, which fixes path issues on systems using +statx (Ubuntu 20.04 in particular).
Quoted 7 lines
So, usually if "pathname" is used, it's intentional to suppress the special magic behavior. Usually, but not always. In this case, the man page does use "pathname" and sometimes that means it's just a cut and paste error.
My man page also says there's no glibc wrapper for statx, making me slightly surprised that a wrapper for it is needed/relevant.
I looked into this. It appears (after pseudo was written, but) about 7 or so years ago, all of the man pages were re-written and the language was changed.
So 'pathname' no longer has the same syntactic meaning that it did when pseudo was first written. (It's not just statx, I looked at things like fopen, open, etc.)
--Mark
Quoted 6 lines
-s
Re: [OE-core] [PATCH 2/2] pseudo: Fix statx function usage
Quoted 3 lines · On Mon, 12 Oct 2020 14:14:36 -0500 "Mark Hatle"…
I looked into this. It appears (after pseudo was written, but) about 7 or so years ago, all of the man pages were re-written and the language was changed.
I don't think I originally intentionally synchronized it with man pages, I just had the convention of special-casing "path", and then started using "pathname" as a convention for "a name that holds a path but we don't want to do the magic on it".
-s
Re: [OE-core] [PATCH 2/2] pseudo: Fix statx function usage
In reply to SeebsQuoted 10 lines · Seebs
Quoted 6 lines · On Wed, 7 Oct 2020 17:20:18 +0100 "Richard Purdie"…
+There is magic in the posts where specific variable names have specific +magic. For that magic to work, "path" needs to be used not
"pathname" as +is currently there. Fix this, which fixes path issues
on systems using +statx (Ubuntu 20.04 in particular).
Quoted 2 lines
So, usually if "pathname" is used, it's intentional to suppress the special magic behavior.
In this case I added the statx wrapper and didn't realise there was magic related to the name "path", so its a bug I introduced. This patch fixes my mistake. I think these are only on the oe-core branch, not master.
Quoted 3 lines
Usually, but not always. In this case, the man
page does use "pathname" and sometimes that means it's just a cut and paste error.
Its my own copy and paste bug :)
Quoted 2 lines
My man page also says there's no glibc wrapper for statx, making me slightly surprised that a wrapper for it is needed/relevant.
Its definitely being used by Ubuntu 20.04 systems as we're findining bugs related to it!
Thanks for the review!
Cheers,
Richard