f21993
commit 23e0e8f5f1fb5ed150253d986ecccdc90c2dcd5e
f21993
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
f21993
Date:   Fri Jan 21 23:32:56 2022 +0530
f21993
f21993
    getcwd: Set errno to ERANGE for size == 1 (CVE-2021-3999)
f21993
    
f21993
    No valid path returned by getcwd would fit into 1 byte, so reject the
f21993
    size early and return NULL with errno set to ERANGE.  This change is
f21993
    prompted by CVE-2021-3999, which describes a single byte buffer
f21993
    underflow and overflow when all of the following conditions are met:
f21993
    
f21993
    - The buffer size (i.e. the second argument of getcwd) is 1 byte
f21993
    - The current working directory is too long
f21993
    - '/' is also mounted on the current working directory
f21993
    
f21993
    Sequence of events:
f21993
    
f21993
    - In sysdeps/unix/sysv/linux/getcwd.c, the syscall returns ENAMETOOLONG
f21993
      because the linux kernel checks for name length before it checks
f21993
      buffer size
f21993
    
f21993
    - The code falls back to the generic getcwd in sysdeps/posix
f21993
    
f21993
    - In the generic func, the buf[0] is set to '\0' on line 250
f21993
    
f21993
    - this while loop on line 262 is bypassed:
f21993
    
f21993
        while (!(thisdev == rootdev && thisino == rootino))
f21993
    
f21993
      since the rootfs (/) is bind mounted onto the directory and the flow
f21993
      goes on to line 449, where it puts a '/' in the byte before the
f21993
      buffer.
f21993
    
f21993
    - Finally on line 458, it moves 2 bytes (the underflowed byte and the
f21993
      '\0') to the buf[0] and buf[1], resulting in a 1 byte buffer overflow.
f21993
    
f21993
    - buf is returned on line 469 and errno is not set.
f21993
    
f21993
    This resolves BZ #28769.
f21993
    
f21993
    Reviewed-by: Andreas Schwab <schwab@linux-m68k.org>
f21993
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
f21993
    Signed-off-by: Qualys Security Advisory <qsa@qualys.com>
f21993
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
f21993
f21993
# Conflicts:
f21993
#	sysdeps/posix/getcwd.c
f21993
#	sysdeps/unix/sysv/linux/Makefile
f21993
f21993
diff --git a/sysdeps/posix/getcwd.c b/sysdeps/posix/getcwd.c
f21993
index b53433a2dc77fafa..fcd7aaea79c6477b 100644
f21993
--- a/sysdeps/posix/getcwd.c
f21993
+++ b/sysdeps/posix/getcwd.c
f21993
@@ -238,6 +238,13 @@ __getcwd (char *buf, size_t size)
f21993
   bool fd_needs_closing = false;
f21993
   int fd = AT_FDCWD;
f21993
 
f21993
+  /* A size of 1 byte is never useful.  */
f21993
+  if (size == 1)
f21993
+    {
f21993
+      __set_errno (ERANGE);
f21993
+      return NULL;
f21993
+    }
f21993
+
f21993
   char *path;
f21993
 #ifndef NO_ALLOCATION
f21993
   size_t allocated = size;
f21993
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
f21993
index 688cf9fa9dea23a6..bb055f9d6b841ff5 100644
f21993
--- a/sysdeps/unix/sysv/linux/Makefile
f21993
+++ b/sysdeps/unix/sysv/linux/Makefile
f21993
@@ -180,7 +180,11 @@ sysdep_routines += xstatconv internal_statvfs internal_statvfs64 \
f21993
 
f21993
 sysdep_headers += bits/fcntl-linux.h
f21993
 
f21993
-tests += tst-fallocate tst-fallocate64
f21993
+tests += \
f21993
+  tst-fallocate \
f21993
+  tst-fallocate64 \
f21993
+  tst-getcwd-smallbuff \
f21993
+# tests
f21993
 endif
f21993
 
f21993
 ifeq ($(subdir),elf)
f21993
diff --git a/sysdeps/unix/sysv/linux/tst-getcwd-smallbuff.c b/sysdeps/unix/sysv/linux/tst-getcwd-smallbuff.c
f21993
new file mode 100644
f21993
index 0000000000000000..d460d6e7662dc5e4
f21993
--- /dev/null
f21993
+++ b/sysdeps/unix/sysv/linux/tst-getcwd-smallbuff.c
f21993
@@ -0,0 +1,241 @@
f21993
+/* Verify that getcwd returns ERANGE for size 1 byte and does not underflow
f21993
+   buffer when the CWD is too long and is also a mount target of /.  See bug
f21993
+   #28769 or CVE-2021-3999 for more context.
f21993
+   Copyright The GNU Toolchain Authors.
f21993
+   This file is part of the GNU C Library.
f21993
+
f21993
+   The GNU C Library is free software; you can redistribute it and/or
f21993
+   modify it under the terms of the GNU Lesser General Public
f21993
+   License as published by the Free Software Foundation; either
f21993
+   version 2.1 of the License, or (at your option) any later version.
f21993
+
f21993
+   The GNU C Library is distributed in the hope that it will be useful,
f21993
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
f21993
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
f21993
+   Lesser General Public License for more details.
f21993
+
f21993
+   You should have received a copy of the GNU Lesser General Public
f21993
+   License along with the GNU C Library; if not, see
f21993
+   <https://www.gnu.org/licenses/>.  */
f21993
+
f21993
+#include <errno.h>
f21993
+#include <fcntl.h>
f21993
+#include <intprops.h>
f21993
+#include <limits.h>
f21993
+#include <stdio.h>
f21993
+#include <stdlib.h>
f21993
+#include <string.h>
f21993
+#include <sys/mount.h>
f21993
+#include <sys/stat.h>
f21993
+#include <sys/types.h>
f21993
+#include <sys/wait.h>
f21993
+
f21993
+#include <sys/socket.h>
f21993
+#include <sys/un.h>
f21993
+#include <support/check.h>
f21993
+#include <support/temp_file.h>
f21993
+#include <support/xsched.h>
f21993
+#include <support/xunistd.h>
f21993
+
f21993
+static char *base;
f21993
+#define BASENAME "tst-getcwd-smallbuff"
f21993
+#define MOUNT_NAME "mpoint"
f21993
+static int sockfd[2];
f21993
+
f21993
+static void
f21993
+do_cleanup (void)
f21993
+{
f21993
+  support_chdir_toolong_temp_directory (base);
f21993
+  TEST_VERIFY_EXIT (rmdir (MOUNT_NAME) == 0);
f21993
+  free (base);
f21993
+}
f21993
+
f21993
+static void
f21993
+send_fd (const int sock, const int fd)
f21993
+{
f21993
+  struct msghdr msg = {0};
f21993
+  union
f21993
+    {
f21993
+      struct cmsghdr hdr;
f21993
+      char buf[CMSG_SPACE (sizeof (int))];
f21993
+    } cmsgbuf = {0};
f21993
+  struct cmsghdr *cmsg;
f21993
+  struct iovec vec;
f21993
+  char ch = 'A';
f21993
+  ssize_t n;
f21993
+
f21993
+  msg.msg_control = &cmsgbuf.buf;
f21993
+  msg.msg_controllen = sizeof (cmsgbuf.buf);
f21993
+
f21993
+  cmsg = CMSG_FIRSTHDR (&msg;;
f21993
+  cmsg->cmsg_len = CMSG_LEN (sizeof (int));
f21993
+  cmsg->cmsg_level = SOL_SOCKET;
f21993
+  cmsg->cmsg_type = SCM_RIGHTS;
f21993
+  memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
f21993
+
f21993
+  vec.iov_base = &ch;
f21993
+  vec.iov_len = 1;
f21993
+  msg.msg_iov = &vec;
f21993
+  msg.msg_iovlen = 1;
f21993
+
f21993
+  while ((n = sendmsg (sock, &msg, 0)) == -1 && errno == EINTR);
f21993
+
f21993
+  TEST_VERIFY_EXIT (n == 1);
f21993
+}
f21993
+
f21993
+static int
f21993
+recv_fd (const int sock)
f21993
+{
f21993
+  struct msghdr msg = {0};
f21993
+  union
f21993
+    {
f21993
+      struct cmsghdr hdr;
f21993
+      char buf[CMSG_SPACE(sizeof(int))];
f21993
+    } cmsgbuf = {0};
f21993
+  struct cmsghdr *cmsg;
f21993
+  struct iovec vec;
f21993
+  ssize_t n;
f21993
+  char ch = '\0';
f21993
+  int fd = -1;
f21993
+
f21993
+  vec.iov_base = &ch;
f21993
+  vec.iov_len = 1;
f21993
+  msg.msg_iov = &vec;
f21993
+  msg.msg_iovlen = 1;
f21993
+
f21993
+  msg.msg_control = &cmsgbuf.buf;
f21993
+  msg.msg_controllen = sizeof (cmsgbuf.buf);
f21993
+
f21993
+  while ((n = recvmsg (sock, &msg, 0)) == -1 && errno == EINTR);
f21993
+  if (n != 1 || ch != 'A')
f21993
+    return -1;
f21993
+
f21993
+  cmsg = CMSG_FIRSTHDR (&msg;;
f21993
+  if (cmsg == NULL)
f21993
+    return -1;
f21993
+  if (cmsg->cmsg_type != SCM_RIGHTS)
f21993
+    return -1;
f21993
+  memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
f21993
+  if (fd < 0)
f21993
+    return -1;
f21993
+  return fd;
f21993
+}
f21993
+
f21993
+static int
f21993
+child_func (void * const arg)
f21993
+{
f21993
+  xclose (sockfd[0]);
f21993
+  const int sock = sockfd[1];
f21993
+  char ch;
f21993
+
f21993
+  TEST_VERIFY_EXIT (read (sock, &ch, 1) == 1);
f21993
+  TEST_VERIFY_EXIT (ch == '1');
f21993
+
f21993
+  if (mount ("/", MOUNT_NAME, NULL, MS_BIND | MS_REC, NULL))
f21993
+    FAIL_EXIT1 ("mount failed: %m\n");
f21993
+  const int fd = xopen ("mpoint",
f21993
+			O_RDONLY | O_PATH | O_DIRECTORY | O_NOFOLLOW, 0);
f21993
+
f21993
+  send_fd (sock, fd);
f21993
+  xclose (fd);
f21993
+
f21993
+  TEST_VERIFY_EXIT (read (sock, &ch, 1) == 1);
f21993
+  TEST_VERIFY_EXIT (ch == 'a');
f21993
+
f21993
+  xclose (sock);
f21993
+  return 0;
f21993
+}
f21993
+
f21993
+static void
f21993
+update_map (char * const mapping, const char * const map_file)
f21993
+{
f21993
+  const size_t map_len = strlen (mapping);
f21993
+
f21993
+  const int fd = xopen (map_file, O_WRONLY, 0);
f21993
+  xwrite (fd, mapping, map_len);
f21993
+  xclose (fd);
f21993
+}
f21993
+
f21993
+static void
f21993
+proc_setgroups_write (const long child_pid, const char * const str)
f21993
+{
f21993
+  const size_t str_len = strlen(str);
f21993
+
f21993
+  char setgroups_path[sizeof ("/proc//setgroups") + INT_STRLEN_BOUND (long)];
f21993
+
f21993
+  snprintf (setgroups_path, sizeof (setgroups_path),
f21993
+	    "/proc/%ld/setgroups", child_pid);
f21993
+
f21993
+  const int fd = open (setgroups_path, O_WRONLY);
f21993
+
f21993
+  if (fd < 0)
f21993
+    {
f21993
+      TEST_VERIFY_EXIT (errno == ENOENT);
f21993
+      FAIL_UNSUPPORTED ("/proc/%ld/setgroups not found\n", child_pid);
f21993
+    }
f21993
+
f21993
+  xwrite (fd, str, str_len);
f21993
+  xclose(fd);
f21993
+}
f21993
+
f21993
+static char child_stack[1024 * 1024];
f21993
+
f21993
+int
f21993
+do_test (void)
f21993
+{
f21993
+  base = support_create_and_chdir_toolong_temp_directory (BASENAME);
f21993
+
f21993
+  xmkdir (MOUNT_NAME, S_IRWXU);
f21993
+  atexit (do_cleanup);
f21993
+
f21993
+  TEST_VERIFY_EXIT (socketpair (AF_UNIX, SOCK_STREAM, 0, sockfd) == 0);
f21993
+  pid_t child_pid = xclone (child_func, NULL, child_stack,
f21993
+			    sizeof (child_stack),
f21993
+			    CLONE_NEWUSER | CLONE_NEWNS | SIGCHLD);
f21993
+
f21993
+  xclose (sockfd[1]);
f21993
+  const int sock = sockfd[0];
f21993
+
f21993
+  char map_path[sizeof ("/proc//uid_map") + INT_STRLEN_BOUND (long)];
f21993
+  char map_buf[sizeof ("0  1") + INT_STRLEN_BOUND (long)];
f21993
+
f21993
+  snprintf (map_path, sizeof (map_path), "/proc/%ld/uid_map",
f21993
+	    (long) child_pid);
f21993
+  snprintf (map_buf, sizeof (map_buf), "0 %ld 1", (long) getuid());
f21993
+  update_map (map_buf, map_path);
f21993
+
f21993
+  proc_setgroups_write ((long) child_pid, "deny");
f21993
+  snprintf (map_path, sizeof (map_path), "/proc/%ld/gid_map",
f21993
+	    (long) child_pid);
f21993
+  snprintf (map_buf, sizeof (map_buf), "0 %ld 1", (long) getgid());
f21993
+  update_map (map_buf, map_path);
f21993
+
f21993
+  TEST_VERIFY_EXIT (send (sock, "1", 1, MSG_NOSIGNAL) == 1);
f21993
+  const int fd = recv_fd (sock);
f21993
+  TEST_VERIFY_EXIT (fd >= 0);
f21993
+  TEST_VERIFY_EXIT (fchdir (fd) == 0);
f21993
+
f21993
+  static char buf[2 * 10 + 1];
f21993
+  memset (buf, 'A', sizeof (buf));
f21993
+
f21993
+  /* Finally, call getcwd and check if it resulted in a buffer underflow.  */
f21993
+  char * cwd = getcwd (buf + sizeof (buf) / 2, 1);
f21993
+  TEST_VERIFY (cwd == NULL);
f21993
+  TEST_VERIFY (errno == ERANGE);
f21993
+
f21993
+  for (int i = 0; i < sizeof (buf); i++)
f21993
+    if (buf[i] != 'A')
f21993
+      {
f21993
+	printf ("buf[%d] = %02x\n", i, (unsigned int) buf[i]);
f21993
+	support_record_failure ();
f21993
+      }
f21993
+
f21993
+  TEST_VERIFY_EXIT (send (sock, "a", 1, MSG_NOSIGNAL) == 1);
f21993
+  xclose (sock);
f21993
+  TEST_VERIFY_EXIT (xwaitpid (child_pid, NULL, 0) == child_pid);
f21993
+
f21993
+  return 0;
f21993
+}
f21993
+
f21993
+#define CLEANUP_HANDLER do_cleanup
f21993
+#include <support/test-driver.c>