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