* procfs readlink after unshare() in a chroot() reports the full path
@ 2022-09-09 10:06 David Laight
2022-09-13 18:44 ` Alexey Dobriyan
0 siblings, 1 reply; 3+ messages in thread
From: David Laight @ 2022-09-09 10:06 UTC (permalink / raw)
To: 'adobriyan@gmail.com'; +Cc: linux-kernel
The readlink calls in procfs (eg for /proc/self/fd/0) returns
the full pathname if unshare() is called inside a chroot.
The program below reproduces this when run with stdin
redirected to a file in the current directory.
This sequence is used by 'ip netns exec' so isn't actually
that unusual.
David
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sched.h>
static void print_link(const char *where, int fd)
{
char buf[256];
printf("%s: %.*s\n", where, (int)readlinkat(fd, "", buf, sizeof buf), buf);
}
int main(int argc, char **argv)
{
int link_fd = open("/proc/self/fd/0", O_PATH | O_NOFOLLOW);
print_link("initial", link_fd);
if (chroot("."))
return 1;
print_link("after chroot", link_fd);
if (unshare(CLONE_NEWNS))
return 2;
print_link("after unshare", link_fd);
return 0;
}
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: procfs readlink after unshare() in a chroot() reports the full path
2022-09-09 10:06 procfs readlink after unshare() in a chroot() reports the full path David Laight
@ 2022-09-13 18:44 ` Alexey Dobriyan
2022-09-14 8:05 ` David Laight
0 siblings, 1 reply; 3+ messages in thread
From: Alexey Dobriyan @ 2022-09-13 18:44 UTC (permalink / raw)
To: David Laight; +Cc: linux-kernel
On Fri, Sep 09, 2022 at 10:06:32AM +0000, David Laight wrote:
> The readlink calls in procfs (eg for /proc/self/fd/0) returns
> the full pathname if unshare() is called inside a chroot.
>
> The program below reproduces this when run with stdin
> redirected to a file in the current directory.
>
> This sequence is used by 'ip netns exec' so isn't actually
> that unusual.
>
> David
>
> #define _GNU_SOURCE
> #include <unistd.h>
> #include <stdio.h>
> #include <fcntl.h>
> #include <sched.h>
>
> static void print_link(const char *where, int fd)
> {
> char buf[256];
>
> printf("%s: %.*s\n", where, (int)readlinkat(fd, "", buf, sizeof buf), buf);
> }
>
> int main(int argc, char **argv)
> {
> int link_fd = open("/proc/self/fd/0", O_PATH | O_NOFOLLOW);
>
> print_link("initial", link_fd);
> if (chroot("."))
> return 1;
> print_link("after chroot", link_fd);
> if (unshare(CLONE_NEWNS))
> return 2;
> print_link("after unshare", link_fd);
> return 0;
> }
I tested mainline and 5.19.8, both are OK:
open("/proc/self/fd/0", O_RDONLY|O_NOFOLLOW|O_PATH) = 3
readlinkat(3, "", "/dev/pts/0", 256) = 10
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd94753e000
write(1, "initial: /dev/pts/0\n", 20initial: /dev/pts/0
) = 20
chroot(".") = 0
readlinkat(3, "", "/dev/pts/0", 256) = 10
write(1, "after chroot: /dev/pts/0\n", 25after chroot: /dev/pts/0
) = 25
unshare(CLONE_NEWNS) = 0
readlinkat(3, "", "/dev/pts/0", 256) = 10
write(1, "after unshare: /dev/pts/0\n", 26after unshare: /dev/pts/0
) = 26
^ permalink raw reply [flat|nested] 3+ messages in thread
* RE: procfs readlink after unshare() in a chroot() reports the full path
2022-09-13 18:44 ` Alexey Dobriyan
@ 2022-09-14 8:05 ` David Laight
0 siblings, 0 replies; 3+ messages in thread
From: David Laight @ 2022-09-14 8:05 UTC (permalink / raw)
To: 'Alexey Dobriyan'; +Cc: linux-kernel
From: Alexey Dobriyan
> Sent: 13 September 2022 19:44
>
> On Fri, Sep 09, 2022 at 10:06:32AM +0000, David Laight wrote:
> > The readlink calls in procfs (eg for /proc/self/fd/0) returns
> > the full pathname if unshare() is called inside a chroot.
> >
> > The program below reproduces this when run with stdin
> > redirected to a file in the current directory.
> >
> > This sequence is used by 'ip netns exec' so isn't actually
> > that unusual.
> >
> > David
> >
> > #define _GNU_SOURCE
> > #include <unistd.h>
> > #include <stdio.h>
> > #include <fcntl.h>
> > #include <sched.h>
> >
> > static void print_link(const char *where, int fd)
> > {
> > char buf[256];
> >
> > printf("%s: %.*s\n", where, (int)readlinkat(fd, "", buf, sizeof buf), buf);
> > }
> >
> > int main(int argc, char **argv)
> > {
> > int link_fd = open("/proc/self/fd/0", O_PATH | O_NOFOLLOW);
> >
> > print_link("initial", link_fd);
> > if (chroot("."))
> > return 1;
> > print_link("after chroot", link_fd);
> > if (unshare(CLONE_NEWNS))
> > return 2;
> > print_link("after unshare", link_fd);
> > return 0;
> > }
>
> I tested mainline and 5.19.8, both are OK:
>
> open("/proc/self/fd/0", O_RDONLY|O_NOFOLLOW|O_PATH) = 3
> readlinkat(3, "", "/dev/pts/0", 256) = 10
> fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
> mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd94753e000
> write(1, "initial: /dev/pts/0\n", 20initial: /dev/pts/0
> ) = 20
> chroot(".") = 0
> readlinkat(3, "", "/dev/pts/0", 256) = 10
> write(1, "after chroot: /dev/pts/0\n", 25after chroot: /dev/pts/0
> ) = 25
> unshare(CLONE_NEWNS) = 0
> readlinkat(3, "", "/dev/pts/0", 256) = 10
> write(1, "after unshare: /dev/pts/0\n", 26after unshare: /dev/pts/0
> ) = 26
You need the path to be inside the chroot.
In some sense "/dev/pts/0" is actually invalid and probably
ought to be tagged as such or an error returned.
So rerun and redirect stdin to a file inside the chroot.
In my original case everything was inside a chroot.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-09-14 8:06 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-09 10:06 procfs readlink after unshare() in a chroot() reports the full path David Laight
2022-09-13 18:44 ` Alexey Dobriyan
2022-09-14 8:05 ` David Laight
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).