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