diff options
Diffstat (limited to 'sysdeps/unix/sysv')
-rw-r--r-- | sysdeps/unix/sysv/linux/Makefile | 3 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/getcwd.c | 8 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/ia64/ipc_priv.h | 21 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/ia64/mmap_internal.h | 29 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/m68k/mmap_internal.h | 2 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/mmap.c | 2 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/mmap_internal.h | 6 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/powerpc/not-errno.h | 30 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/tst-ttyname.c | 577 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/ttyname.c | 63 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/ttyname.h | 18 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/ttyname_r.c | 65 |
12 files changed, 728 insertions, 96 deletions
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index dbe6a36c57..b1fe960d00 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -50,7 +50,8 @@ sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \ bits/siginfo-arch.h bits/siginfo-consts-arch.h tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \ - tst-quota tst-sync_file_range test-errno-linux tst-sysconf-iov_max + tst-quota tst-sync_file_range tst-sysconf-iov_max tst-ttyname \ + test-errno-linux # Generate the list of SYS_* macros for the system calls (__NR_* macros). diff --git a/sysdeps/unix/sysv/linux/getcwd.c b/sysdeps/unix/sysv/linux/getcwd.c index 3b556fd450..f485de88a5 100644 --- a/sysdeps/unix/sysv/linux/getcwd.c +++ b/sysdeps/unix/sysv/linux/getcwd.c @@ -76,7 +76,7 @@ __getcwd (char *buf, size_t size) int retval; retval = INLINE_SYSCALL (getcwd, 2, path, alloc_size); - if (retval >= 0) + if (retval > 0 && path[0] == '/') { #ifndef NO_ALLOCATION if (buf == NULL && size == 0) @@ -92,10 +92,10 @@ __getcwd (char *buf, size_t size) return buf; } - /* The system call cannot handle paths longer than a page. - Neither can the magic symlink in /proc/self. Just use the + /* The system call either cannot handle paths longer than a page + or can succeed without returning an absolute path. Just use the generic implementation right away. */ - if (errno == ENAMETOOLONG) + if (retval >= 0 || errno == ENAMETOOLONG) { #ifndef NO_ALLOCATION if (buf == NULL && size == 0) diff --git a/sysdeps/unix/sysv/linux/ia64/ipc_priv.h b/sysdeps/unix/sysv/linux/ia64/ipc_priv.h new file mode 100644 index 0000000000..e602eea455 --- /dev/null +++ b/sysdeps/unix/sysv/linux/ia64/ipc_priv.h @@ -0,0 +1,21 @@ +/* Old SysV permission definition for Linux. IA64 version. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#include <sys/ipc.h> /* For __key_t */ + +#define __IPC_64 0x0 diff --git a/sysdeps/unix/sysv/linux/ia64/mmap_internal.h b/sysdeps/unix/sysv/linux/ia64/mmap_internal.h new file mode 100644 index 0000000000..dbaaa3f904 --- /dev/null +++ b/sysdeps/unix/sysv/linux/ia64/mmap_internal.h @@ -0,0 +1,29 @@ +/* Common mmap definition for Linux implementation. Linux/ia64 version. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#ifndef MMAP_IA64_INTERNAL_LINUX_H +#define MMAP_IA64_INTERNAL_LINUX_H + +/* Linux allows PAGE_SHIFT in range of [12-16] and expect + mmap2 offset to be provided in based on the configured pagesize. + Determine the shift dynamically with getpagesize. */ +#define MMAP2_PAGE_UNIT -1 + +#include_next <mmap_internal.h> + +#endif diff --git a/sysdeps/unix/sysv/linux/m68k/mmap_internal.h b/sysdeps/unix/sysv/linux/m68k/mmap_internal.h index bd8bd3843b..9fe9d9127d 100644 --- a/sysdeps/unix/sysv/linux/m68k/mmap_internal.h +++ b/sysdeps/unix/sysv/linux/m68k/mmap_internal.h @@ -22,7 +22,7 @@ /* ColdFire and Sun 3 kernels have PAGE_SHIFT set to 13 and expect mmap2 offset to be provided in 8K pages. Determine the shift dynamically with getpagesize. */ -#define MMAP2_PAGE_SHIFT -1 +#define MMAP2_PAGE_UNIT -1 #include_next <mmap_internal.h> diff --git a/sysdeps/unix/sysv/linux/mmap.c b/sysdeps/unix/sysv/linux/mmap.c index 98c2f88eb8..59292b6d79 100644 --- a/sysdeps/unix/sysv/linux/mmap.c +++ b/sysdeps/unix/sysv/linux/mmap.c @@ -21,9 +21,9 @@ #include <sys/mman.h> #include <sysdep.h> #include <stdint.h> -#include <mmap_internal.h> #ifndef __OFF_T_MATCHES_OFF64_T +# include <mmap_internal.h> /* An architecture may override this. */ # ifndef MMAP_ADJUST_OFFSET diff --git a/sysdeps/unix/sysv/linux/mmap_internal.h b/sysdeps/unix/sysv/linux/mmap_internal.h index 499e389605..47c099183c 100644 --- a/sysdeps/unix/sysv/linux/mmap_internal.h +++ b/sysdeps/unix/sysv/linux/mmap_internal.h @@ -27,13 +27,13 @@ #endif #if MMAP2_PAGE_UNIT == -1 -static int page_unit; - +static uint64_t page_unit; # define MMAP_CHECK_PAGE_UNIT() \ if (page_unit == 0) \ page_unit = __getpagesize (); +# undef MMAP2_PAGE_UNIT +# define MMAP2_PAGE_UNIT page_unit #else -# define page_unit MMAP2_PAGE_UNIT # define MMAP_CHECK_PAGE_UNIT() #endif diff --git a/sysdeps/unix/sysv/linux/powerpc/not-errno.h b/sysdeps/unix/sysv/linux/powerpc/not-errno.h new file mode 100644 index 0000000000..27da21bdf1 --- /dev/null +++ b/sysdeps/unix/sysv/linux/powerpc/not-errno.h @@ -0,0 +1,30 @@ +/* Syscall wrapper that do not set errno. Linux powerpc version. + Copyright (C) 2018 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +/* __access_noerrno is used during process initialization in elf/dl-tunables.c + before the TCB is initialized, prohibiting the usage of + ABORT_TRANSACTION. */ +#undef ABORT_TRANSACTION +#define ABORT_TRANSACTION + +#include "sysdeps/unix/sysv/linux/not-errno.h" + +/* Recover ABORT_TRANSACTION's previous value, in order to not affect + other syscalls. */ +#undef ABORT_TRANSACTION +#define ABORT_TRANSACTION ABORT_TRANSACTION_IMPL diff --git a/sysdeps/unix/sysv/linux/tst-ttyname.c b/sysdeps/unix/sysv/linux/tst-ttyname.c new file mode 100644 index 0000000000..6848a6d3e2 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-ttyname.c @@ -0,0 +1,577 @@ +/* Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; see the file COPYING.LIB. If + not, see <http://www.gnu.org/licenses/>. */ + +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <sched.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mount.h> +#include <sys/prctl.h> +#include <sys/stat.h> +#include <sys/wait.h> +#include <unistd.h> + +#include <support/check.h> +#include <support/namespace.h> +#include <support/support.h> +#include <support/temp_file.h> +#include <support/test-driver.h> +#include <support/xunistd.h> + +/* generic utilities */ + +#define VERIFY(expr) \ + do { \ + if (!(expr)) \ + { \ + printf ("error: %s:%d: %s: %m\n", \ + __FILE__, __LINE__, #expr); \ + exit (1); \ + } \ + } while (0) + +static void +touch (const char *path, mode_t mode) +{ + xclose (xopen (path, O_WRONLY|O_CREAT|O_NOCTTY, mode)); +} + +static size_t +trim_prefix (char *str, size_t str_len, const char *prefix) +{ + size_t prefix_len = strlen (prefix); + if (str_len > prefix_len && memcmp (str, prefix, prefix_len) == 0) + { + memmove (str, str + prefix_len, str_len - prefix_len); + return str_len - prefix_len; + } + return str_len; +} + +/* returns a pointer to static storage */ +static char * +proc_fd_readlink (const char *linkname) +{ + static char target[PATH_MAX+1]; + ssize_t target_len = readlink (linkname, target, PATH_MAX); + VERIFY (target_len > 0); + target_len = trim_prefix (target, target_len, "(unreachable)"); + target[target_len] = '\0'; + return target; +} + +/* plain ttyname runner */ + +struct result +{ + const char *name; + int err; +}; + +/* strings in result structure are in static storage */ +static struct result +run_ttyname (int fd) +{ + struct result ret; + errno = 0; + ret.name = ttyname (fd); + ret.err = errno; + return ret; +} + +static bool +eq_ttyname (struct result actual, struct result expected) +{ + char *actual_name, *expected_name; + + if ((actual.err == expected.err) && + (!actual.name == !expected.name) && + (actual.name ? strcmp (actual.name, expected.name) == 0 : true)) + { + if (expected.name) + expected_name = xasprintf ("\"%s\"", expected.name); + else + expected_name = xstrdup ("NULL"); + + printf ("info: ttyname: PASS {name=%s, errno=%d}\n", + expected_name, expected.err); + + free (expected_name); + return true; + } + + if (actual.name) + actual_name = xasprintf ("\"%s\"", actual.name); + else + actual_name = xstrdup ("NULL"); + + if (expected.name) + expected_name = xasprintf ("\"%s\"", expected.name); + else + expected_name = xstrdup ("NULL"); + + printf ("error: ttyname: actual {name=%s, errno=%d} != expected {name=%s, errno=%d}\n", + actual_name, actual.err, + expected_name, expected.err); + + free (actual_name); + free (expected_name); + return false; +} + +/* ttyname_r runner */ + +struct result_r +{ + const char *name; + int ret; + int err; +}; + +/* strings in result structure are in static storage */ +static struct result_r +run_ttyname_r (int fd) +{ + static char buf[TTY_NAME_MAX]; + + struct result_r ret; + errno = 0; + ret.ret = ttyname_r (fd, buf, TTY_NAME_MAX); + ret.err = errno; + if (ret.ret == 0) + ret.name = buf; + else + ret.name = NULL; + return ret; +} + +static bool +eq_ttyname_r (struct result_r actual, struct result_r expected) +{ + char *actual_name, *expected_name; + + if ((actual.err == expected.err) && + (actual.ret == expected.ret) && + (!actual.name == !expected.name) && + (actual.name ? strcmp (actual.name, expected.name) == 0 : true)) + { + if (expected.name) + expected_name = xasprintf ("\"%s\"", expected.name); + else + expected_name = xstrdup ("NULL"); + + printf ("info: ttyname_r: PASS {name=%s, ret=%d, errno=%d}\n", + expected_name, expected.ret, expected.err); + + free (expected_name); + return true; + } + + if (actual.name) + actual_name = xasprintf ("\"%s\"", actual.name); + else + actual_name = xstrdup ("NULL"); + + if (expected.name) + expected_name = xasprintf ("\"%s\"", expected.name); + else + expected_name = xstrdup ("NULL"); + + printf ("error: ttyname_r: actual {name=%s, ret=%d, errno=%d} != expected {name=%s, ret=%d, errno=%d}\n", + actual_name, actual.ret, actual.err, + expected_name, expected.ret, expected.err); + + free (actual_name); + free (expected_name); + return false; +} + +/* combined runner */ + +static bool +doit (int fd, const char *testname, struct result_r expected_r) +{ + struct result expected = {.name=expected_r.name, .err=expected_r.ret}; + bool ret = true; + + printf ("info: testcase: %s\n", testname); + + if (!eq_ttyname (run_ttyname (fd), expected)) + ret = false; + if (!eq_ttyname_r (run_ttyname_r (fd), expected_r)) + ret = false; + + if (!ret) + support_record_failure (); + + return ret; +} + +/* chroot setup */ + +static char *chrootdir; + +static void +prepare (int argc, char **argv) +{ + chrootdir = xasprintf ("%s/tst-ttyname-XXXXXX", test_dir); + if (mkdtemp (chrootdir) == NULL) + FAIL_EXIT1 ("mkdtemp (\"%s\"): %m", chrootdir); + add_temp_file (chrootdir); +} +#define PREPARE prepare + +/* These chroot setup functions put the TTY at at "/console" (where it + won't be found by ttyname), and create "/dev/console" as an + ordinary file. This way, it's easier to write test-cases that + expect ttyname to fail; test-cases that expect it to succeed need + to explicitly remount it at "/dev/console". */ + +static int +do_in_chroot_1 (int (*cb)(const char *, int)) +{ + printf ("info: entering chroot 1\n"); + + /* Open the PTS that we'll be testing on. */ + int master; + char *slavename; + master = posix_openpt (O_RDWR|O_NOCTTY|O_NONBLOCK); + if (master < 0) + { + if (errno == ENOENT) + FAIL_UNSUPPORTED ("posix_openpt: %m"); + else + FAIL_EXIT1 ("posix_openpt: %m"); + } + VERIFY ((slavename = ptsname (master))); + VERIFY (unlockpt (master) == 0); + if (strncmp (slavename, "/dev/pts/", 9) != 0) + FAIL_UNSUPPORTED ("slave pseudo-terminal is not under /dev/pts/: %s", + slavename); + int slave = xopen (slavename, O_RDWR, 0); + if (!doit (slave, "basic smoketest", + (struct result_r){.name=slavename, .ret=0, .err=0})) + return 1; + + pid_t pid = xfork (); + if (pid == 0) + { + xclose (master); + + if (!support_enter_mount_namespace ()) + FAIL_UNSUPPORTED ("could not enter new mount namespace"); + + VERIFY (mount ("tmpfs", chrootdir, "tmpfs", 0, "mode=755") == 0); + VERIFY (chdir (chrootdir) == 0); + + xmkdir ("proc", 0755); + xmkdir ("dev", 0755); + xmkdir ("dev/pts", 0755); + + VERIFY (mount ("/proc", "proc", NULL, MS_BIND|MS_REC, NULL) == 0); + VERIFY (mount ("devpts", "dev/pts", "devpts", + MS_NOSUID|MS_NOEXEC, + "newinstance,ptmxmode=0666,mode=620") == 0); + VERIFY (symlink ("pts/ptmx", "dev/ptmx") == 0); + + touch ("console", 0); + touch ("dev/console", 0); + VERIFY (mount (slavename, "console", NULL, MS_BIND, NULL) == 0); + + xchroot ("."); + + char *linkname = xasprintf ("/proc/self/fd/%d", slave); + char *target = proc_fd_readlink (linkname); + VERIFY (strcmp (target, slavename) == 0); + free (linkname); + + _exit (cb (slavename, slave)); + } + int status; + xwaitpid (pid, &status, 0); + VERIFY (WIFEXITED (status)); + xclose (master); + xclose (slave); + return WEXITSTATUS (status); +} + +static int +do_in_chroot_2 (int (*cb)(const char *, int)) +{ + printf ("info: entering chroot 2\n"); + + int pid_pipe[2]; + xpipe (pid_pipe); + int exit_pipe[2]; + xpipe (exit_pipe); + + /* Open the PTS that we'll be testing on. */ + int master; + char *slavename; + VERIFY ((master = posix_openpt (O_RDWR|O_NOCTTY|O_NONBLOCK)) >= 0); + VERIFY ((slavename = ptsname (master))); + VERIFY (unlockpt (master) == 0); + if (strncmp (slavename, "/dev/pts/", 9) != 0) + FAIL_UNSUPPORTED ("slave pseudo-terminal is not under /dev/pts/: %s", + slavename); + /* wait until in a new mount ns to open the slave */ + + /* enable `wait`ing on grandchildren */ + VERIFY (prctl (PR_SET_CHILD_SUBREAPER, 1) == 0); + + pid_t pid = xfork (); /* outer child */ + if (pid == 0) + { + xclose (master); + xclose (pid_pipe[0]); + xclose (exit_pipe[1]); + + if (!support_enter_mount_namespace ()) + FAIL_UNSUPPORTED ("could not enter new mount namespace"); + + int slave = xopen (slavename, O_RDWR, 0); + if (!doit (slave, "basic smoketest", + (struct result_r){.name=slavename, .ret=0, .err=0})) + _exit (1); + + VERIFY (mount ("tmpfs", chrootdir, "tmpfs", 0, "mode=755") == 0); + VERIFY (chdir (chrootdir) == 0); + + xmkdir ("proc", 0755); + xmkdir ("dev", 0755); + xmkdir ("dev/pts", 0755); + + VERIFY (mount ("devpts", "dev/pts", "devpts", + MS_NOSUID|MS_NOEXEC, + "newinstance,ptmxmode=0666,mode=620") == 0); + VERIFY (symlink ("pts/ptmx", "dev/ptmx") == 0); + + touch ("console", 0); + touch ("dev/console", 0); + VERIFY (mount (slavename, "console", NULL, MS_BIND, NULL) == 0); + + xchroot ("."); + + if (unshare (CLONE_NEWNS | CLONE_NEWPID) < 0) + FAIL_UNSUPPORTED ("could not enter new PID namespace"); + pid = xfork (); /* inner child */ + if (pid == 0) + { + xclose (pid_pipe[1]); + + /* wait until the outer child has exited */ + char c; + VERIFY (read (exit_pipe[0], &c, 1) == 0); + xclose (exit_pipe[0]); + + VERIFY (mount ("proc", "/proc", "proc", + MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL) == 0); + + char *linkname = xasprintf ("/proc/self/fd/%d", slave); + char *target = proc_fd_readlink (linkname); + VERIFY (strcmp (target, strrchr (slavename, '/')) == 0); + free (linkname); + + _exit (cb (slavename, slave)); + } + xwrite (pid_pipe[1], &pid, sizeof pid); + _exit (0); + } + xclose (pid_pipe[1]); + xclose (exit_pipe[0]); + xclose (exit_pipe[1]); + + /* wait for the outer child */ + int status; + xwaitpid (pid, &status, 0); + VERIFY (WIFEXITED (status)); + int ret = WEXITSTATUS (status); + if (ret != 0) + return ret; + + /* set 'pid' to the inner child */ + VERIFY (read (pid_pipe[0], &pid, sizeof pid) == sizeof pid); + xclose (pid_pipe[0]); + + /* wait for the inner child */ + xwaitpid (pid, &status, 0); + VERIFY (WIFEXITED (status)); + xclose (master); + return WEXITSTATUS (status); +} + +/* main test */ + +static int +run_chroot_tests (const char *slavename, int slave) +{ + struct stat st; + bool ok = true; + + /* There are 3 groups of tests here. The first group fairly + generically does things known to mess up ttyname, and verifies + that ttyname copes correctly. The remaining groups are + increasingly convoluted, as we target specific parts of ttyname + to try to confuse. */ + + /* Basic tests that it doesn't get confused by multiple devpts + instances. */ + { + VERIFY (stat (slavename, &st) < 0); /* sanity check */ + if (!doit (slave, "no conflict, no match", + (struct result_r){.name=NULL, .ret=ENODEV, .err=ENODEV})) + ok = false; + VERIFY (mount ("/console", "/dev/console", NULL, MS_BIND, NULL) == 0); + if (!doit (slave, "no conflict, console", + (struct result_r){.name="/dev/console", .ret=0, .err=0})) + ok = false; + VERIFY (umount ("/dev/console") == 0); + + /* keep creating PTYs until we we get a name collision */ + while (stat (slavename, &st) < 0) + posix_openpt (O_RDWR|O_NOCTTY|O_NONBLOCK); + VERIFY (stat (slavename, &st) == 0); + + if (!doit (slave, "conflict, no match", + (struct result_r){.name=NULL, .ret=ENODEV, .err=ENODEV})) + ok = false; + VERIFY (mount ("/console", "/dev/console", NULL, MS_BIND, NULL) == 0); + if (!doit (slave, "conflict, console", + (struct result_r){.name="/dev/console", .ret=0, .err=0})) + ok = false; + VERIFY (umount ("/dev/console") == 0); + } + + /* The first tests kinda assumed that they hit certain code-paths + based on assuming that the readlink target is 'slavename', but + that's not quite always true. They're still a good preliminary + sanity check, so keep them, but let's add tests that make sure + that those code-paths are hit by doing a readlink ourself. */ + { + char *linkname = xasprintf ("/proc/self/fd/%d", slave); + char *target = proc_fd_readlink (linkname); + free (linkname); + /* Depeding on how we set up the chroot, the kernel may or may not + trim the leading path to the target (it may give us "/6", + instead of "/dev/pts/6"). We test it both ways (do_in_chroot_1 + and do_in_chroot_2). This test group relies on the target + existing, so guarantee that it does exist by creating it if + necessary. */ + if (stat (target, &st) < 0) + { + VERIFY (errno == ENOENT); + touch (target, 0); + } + + VERIFY (mount ("/console", "/dev/console", NULL, MS_BIND, NULL) == 0); + VERIFY (mount ("/console", target, NULL, MS_BIND, NULL) == 0); + if (!doit (slave, "with readlink target", + (struct result_r){.name=target, .ret=0, .err=0})) + ok = false; + VERIFY (umount (target) == 0); + VERIFY (umount ("/dev/console") == 0); + + VERIFY (mount ("/console", "/dev/console", NULL, MS_BIND, NULL) == 0); + VERIFY (mount (slavename, target, NULL, MS_BIND, NULL) == 0); + if (!doit (slave, "with readlink trap; fallback", + (struct result_r){.name="/dev/console", .ret=0, .err=0})) + ok = false; + VERIFY (umount (target) == 0); + VERIFY (umount ("/dev/console") == 0); + + VERIFY (mount (slavename, target, NULL, MS_BIND, NULL) == 0); + if (!doit (slave, "with readlink trap; no fallback", + (struct result_r){.name=NULL, .ret=ENODEV, .err=ENODEV})) + ok = false; + VERIFY (umount (target) == 0); + } + + /* This test makes sure that everything still works OK if readdir + finds a pseudo-match before and/or after the actual match. Now, + to do that, we need to control that readdir finds the + pseudo-matches before and after the actual match; and there's no + good way to control that order in absence of whitebox testing. + So, just create 3 files, then use opendir/readdir to see what + order they are in, and assign meaning based on that order, not by + name; assigning the first to be a pseudo-match, the second to be + the actual match, and the third to be a pseudo-match. This + assumes that (on tmpfs) ordering within the directory is stable + in the absence of modification, which seems reasonably safe. */ + { + /* since we're testing the fallback search, disable the readlink + happy-path */ + VERIFY (umount2 ("/proc", MNT_DETACH) == 0); + + touch ("/dev/console1", 0); + touch ("/dev/console2", 0); + touch ("/dev/console3", 0); + + char *c[3]; + int ci = 0; + DIR *dirstream = opendir ("/dev"); + VERIFY (dirstream != NULL); + struct dirent *d; + while ((d = readdir (dirstream)) != NULL && ci < 3) + { + if (strcmp (d->d_name, "console1") && + strcmp (d->d_name, "console2") && + strcmp (d->d_name, "console3") ) + continue; + c[ci++] = xasprintf ("/dev/%s", d->d_name); + } + VERIFY (ci == 3); + VERIFY (closedir (dirstream) == 0); + + VERIFY (mount (slavename, c[0], NULL, MS_BIND, NULL) == 0); + VERIFY (mount ("/console", c[1], NULL, MS_BIND, NULL) == 0); + VERIFY (mount (slavename, c[2], NULL, MS_BIND, NULL) == 0); + VERIFY (umount2 ("/dev/pts", MNT_DETACH) == 0); + if (!doit (slave, "with search-path trap", + (struct result_r){.name=c[1], .ret=0, .err=0})) + ok = false; + for (int i = 0; i < 3; i++) + { + VERIFY (umount (c[i]) == 0); + VERIFY (unlink (c[i]) == 0); + free (c[i]); + } + } + + return ok ? 0 : 1; +} + +static int +do_test (void) +{ + support_become_root (); + + int ret1 = do_in_chroot_1 (run_chroot_tests); + if (ret1 == EXIT_UNSUPPORTED) + return ret1; + + int ret2 = do_in_chroot_2 (run_chroot_tests); + if (ret2 == EXIT_UNSUPPORTED) + return ret2; + + return ret1 | ret2; +} + +#include <support/test-driver.c> diff --git a/sysdeps/unix/sysv/linux/ttyname.c b/sysdeps/unix/sysv/linux/ttyname.c index 5909cb765f..f4c955f25b 100644 --- a/sysdeps/unix/sysv/linux/ttyname.c +++ b/sysdeps/unix/sysv/linux/ttyname.c @@ -35,16 +35,14 @@ char *__ttyname; #endif -static char *getttyname (const char *dev, dev_t mydev, - ino64_t myino, int save, int *dostat) - internal_function; - +static char *getttyname (const char *dev, const struct stat64 *mytty, + int save, int *dostat); libc_freeres_ptr (static char *getttyname_name); static char * -internal_function attribute_compat_text_section -getttyname (const char *dev, dev_t mydev, ino64_t myino, int save, int *dostat) +attribute_compat_text_section +getttyname (const char *dev, const struct stat64 *mytty, int save, int *dostat) { static size_t namelen; struct stat64 st; @@ -65,7 +63,7 @@ getttyname (const char *dev, dev_t mydev, ino64_t myino, int save, int *dostat) *((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/'; while ((d = __readdir64 (dirstream)) != NULL) - if ((d->d_fileno == myino || *dostat) + if ((d->d_fileno == mytty->st_ino || *dostat) && strcmp (d->d_name, "stdin") && strcmp (d->d_name, "stdout") && strcmp (d->d_name, "stderr")) @@ -87,12 +85,7 @@ getttyname (const char *dev, dev_t mydev, ino64_t myino, int save, int *dostat) } memcpy (&getttyname_name[devlen], d->d_name, dlen); if (__xstat64 (_STAT_VER, getttyname_name, &st) == 0 -#ifdef _STATBUF_ST_RDEV - && S_ISCHR (st.st_mode) && st.st_rdev == mydev -#else - && d->d_fileno == myino && st.st_dev == mydev -#endif - ) + && is_mytty (mytty, &st)) { (void) __closedir (dirstream); #if 0 @@ -122,6 +115,7 @@ ttyname (int fd) char procname[30]; struct stat64 st, st1; int dostat = 0; + int doispty = 0; char *name; int save = errno; struct termios term; @@ -169,30 +163,15 @@ ttyname (int fd) /* Verify readlink result, fall back on iterating through devices. */ if (ttyname_buf[0] == '/' && __xstat64 (_STAT_VER, ttyname_buf, &st1) == 0 -#ifdef _STATBUF_ST_RDEV - && S_ISCHR (st1.st_mode) - && st1.st_rdev == st.st_rdev -#endif - && st1.st_ino == st.st_ino - && st1.st_dev == st.st_dev) + && is_mytty (&st, &st1)) return ttyname_buf; - /* If the link doesn't exist, then it points to a device in another - namespace. */ - if (is_pty (&st)) - { - __set_errno (ENODEV); - return NULL; - } + doispty = 1; } if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode)) { -#ifdef _STATBUF_ST_RDEV - name = getttyname ("/dev/pts", st.st_rdev, st.st_ino, save, &dostat); -#else - name = getttyname ("/dev/pts", st.st_dev, st.st_ino, save, &dostat); -#endif + name = getttyname ("/dev/pts", &st, save, &dostat); } else { @@ -202,21 +181,23 @@ ttyname (int fd) if (!name && dostat != -1) { -#ifdef _STATBUF_ST_RDEV - name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat); -#else - name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat); -#endif + name = getttyname ("/dev", &st, save, &dostat); } if (!name && dostat != -1) { dostat = 1; -#ifdef _STATBUF_ST_RDEV - name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat); -#else - name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat); -#endif + name = getttyname ("/dev", &st, save, &dostat); + } + + if (!name && doispty && is_pty (&st)) + { + /* We failed to figure out the TTY's name, but we can at least + signal that we did verify that it really is a PTY slave. + This happens when we have inherited the file descriptor from + a different mount namespace. */ + __set_errno (ENODEV); + return NULL; } return name; diff --git a/sysdeps/unix/sysv/linux/ttyname.h b/sysdeps/unix/sysv/linux/ttyname.h index 2e415e4e9c..48181330a9 100644 --- a/sysdeps/unix/sysv/linux/ttyname.h +++ b/sysdeps/unix/sysv/linux/ttyname.h @@ -16,13 +16,15 @@ not, see <http://www.gnu.org/licenses/>. */ #include <unistd.h> +#include <stdbool.h> #include <sys/sysmacros.h> #include <sys/types.h> #include <sys/stat.h> /* Return true if this is a UNIX98 pty device, as defined in - linux/Documentation/devices.txt. */ -static inline int + linux/Documentation/devices.txt (on linux < 4.10) or + linux/Documentation/admin-guide/devices.txt (on linux >= 4.10). */ +static inline bool is_pty (struct stat64 *sb) { #ifdef _STATBUF_ST_RDEV @@ -32,3 +34,15 @@ is_pty (struct stat64 *sb) return false; #endif } + +static inline bool +is_mytty (const struct stat64 *mytty, const struct stat64 *maybe) +{ + return (maybe->st_ino == mytty->st_ino + && maybe->st_dev == mytty->st_dev +#ifdef _STATBUF_ST_RDEV + && S_ISCHR (maybe->st_mode) + && maybe->st_rdev == mytty->st_rdev +#endif + ); +} diff --git a/sysdeps/unix/sysv/linux/ttyname_r.c b/sysdeps/unix/sysv/linux/ttyname_r.c index dc863526ba..00eefc2c5c 100644 --- a/sysdeps/unix/sysv/linux/ttyname_r.c +++ b/sysdeps/unix/sysv/linux/ttyname_r.c @@ -31,12 +31,12 @@ #include "ttyname.h" static int getttyname_r (char *buf, size_t buflen, - dev_t mydev, ino64_t myino, int save, - int *dostat) internal_function; + const struct stat64 *mytty, int save, + int *dostat); static int -internal_function attribute_compat_text_section -getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino, +attribute_compat_text_section +getttyname_r (char *buf, size_t buflen, const struct stat64 *mytty, int save, int *dostat) { struct stat64 st; @@ -52,7 +52,7 @@ getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino, } while ((d = __readdir64 (dirstream)) != NULL) - if ((d->d_fileno == myino || *dostat) + if ((d->d_fileno == mytty->st_ino || *dostat) && strcmp (d->d_name, "stdin") && strcmp (d->d_name, "stdout") && strcmp (d->d_name, "stderr")) @@ -72,12 +72,7 @@ getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino, cp[0] = '\0'; if (__xstat64 (_STAT_VER, buf, &st) == 0 -#ifdef _STATBUF_ST_RDEV - && S_ISCHR (st.st_mode) && st.st_rdev == mydev -#else - && d->d_fileno == myino && st.st_dev == mydev -#endif - ) + && is_mytty (mytty, &st)) { (void) __closedir (dirstream); __set_errno (save); @@ -100,6 +95,7 @@ __ttyname_r (int fd, char *buf, size_t buflen) char procname[30]; struct stat64 st, st1; int dostat = 0; + int doispty = 0; int save = errno; /* Test for the absolute minimal size. This makes life easier inside @@ -151,22 +147,10 @@ __ttyname_r (int fd, char *buf, size_t buflen) /* Verify readlink result, fall back on iterating through devices. */ if (buf[0] == '/' && __xstat64 (_STAT_VER, buf, &st1) == 0 -#ifdef _STATBUF_ST_RDEV - && S_ISCHR (st1.st_mode) - && st1.st_rdev == st.st_rdev -#endif - && st1.st_ino == st.st_ino - && st1.st_dev == st.st_dev) + && is_mytty (&st, &st1)) return 0; - /* If the link doesn't exist, then it points to a device in another - * namespace. - */ - if (is_pty (&st)) - { - __set_errno (ENODEV); - return ENODEV; - } + doispty = 1; } /* Prepare the result buffer. */ @@ -175,13 +159,8 @@ __ttyname_r (int fd, char *buf, size_t buflen) if (__xstat64 (_STAT_VER, buf, &st1) == 0 && S_ISDIR (st1.st_mode)) { -#ifdef _STATBUF_ST_RDEV - ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save, + ret = getttyname_r (buf, buflen, &st, save, &dostat); -#else - ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save, - &dostat); -#endif } else { @@ -193,26 +172,26 @@ __ttyname_r (int fd, char *buf, size_t buflen) { buf[sizeof ("/dev/") - 1] = '\0'; buflen += sizeof ("pts/") - 1; -#ifdef _STATBUF_ST_RDEV - ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save, - &dostat); -#else - ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save, + ret = getttyname_r (buf, buflen, &st, save, &dostat); -#endif } if (ret && dostat != -1) { buf[sizeof ("/dev/") - 1] = '\0'; dostat = 1; -#ifdef _STATBUF_ST_RDEV - ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, + ret = getttyname_r (buf, buflen, &st, save, &dostat); -#else - ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, - save, &dostat); -#endif + } + + if (ret && doispty && is_pty (&st)) + { + /* We failed to figure out the TTY's name, but we can at least + signal that we did verify that it really is a PTY slave. + This happens when we have inherited the file descriptor from + a different mount namespace. */ + __set_errno (ENODEV); + return ENODEV; } return ret; |