From: David Laight <David.Laight@ACULAB.COM>
To: "'adobriyan@gmail.com'" <adobriyan@gmail.com>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: procfs readlink after unshare() in a chroot() reports the full path
Date: Fri, 9 Sep 2022 10:06:32 +0000 [thread overview]
Message-ID: <12add75b103b412494487518c408fe0b@AcuMS.aculab.com> (raw)
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)
next reply other threads:[~2022-09-09 10:06 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-09 10:06 David Laight [this message]
2022-09-13 18:44 ` Alexey Dobriyan
2022-09-14 8:05 ` David Laight
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=12add75b103b412494487518c408fe0b@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=adobriyan@gmail.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®