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