lkml.org 
[lkml]   [2022]   [Sep]   [9]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Subjectprocfs readlink after unshare() in a chroot() reports the full path
Date
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)

\
 
 \ /
  Last update: 2022-09-09 12:07    [W:1.592 / U:0.500 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site