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