summaryrefslogtreecommitdiffstats
path: root/meta/recipes-core/glibc/glibc
diff options
context:
space:
mode:
authorPgowda <pgowda.cve@gmail.com>2022-01-24 05:10:43 -0800
committerRichard Purdie <richard.purdie@linuxfoundation.org>2022-01-26 06:27:00 +0000
commit603dffc49a2c0084ce73340fdf1d7c624d1a954e (patch)
treec4cd0f6ad5ed2264c04e4b9dbf6361376d029a0e /meta/recipes-core/glibc/glibc
parent359fcb9ed29c73a39b96aded1a501815727dfd95 (diff)
downloadpoky-603dffc49a2c0084ce73340fdf1d7c624d1a954e.tar.gz
glibc : Fix CVE-2021-3999
Upstream-Status: Backport [https://sourceware.org/git/?p=glibc.git;a=commit;h=8c8a71c85f2ed5cc90d08d82ce645513fc907cb6] Upstream-Status: Backport [https://sourceware.org/git/?p=glibc.git;a=commit;h=472e799a5f2102bc0c3206dbd5a801765fceb39c] (From OE-Core rev: e9532134b86211801206ff540c4c284f43006f7b) Signed-off-by: pgowda <pgowda.cve@gmail.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/recipes-core/glibc/glibc')
-rw-r--r--meta/recipes-core/glibc/glibc/0001-CVE-2021-3999.patch36
-rw-r--r--meta/recipes-core/glibc/glibc/0002-CVE-2021-3999.patch357
2 files changed, 393 insertions, 0 deletions
diff --git a/meta/recipes-core/glibc/glibc/0001-CVE-2021-3999.patch b/meta/recipes-core/glibc/glibc/0001-CVE-2021-3999.patch
new file mode 100644
index 0000000000..64749390b5
--- /dev/null
+++ b/meta/recipes-core/glibc/glibc/0001-CVE-2021-3999.patch
@@ -0,0 +1,36 @@
1From 8c8a71c85f2ed5cc90d08d82ce645513fc907cb6 Mon Sep 17 00:00:00 2001
2From: Siddhesh Poyarekar <siddhesh@sourceware.org>
3Date: Mon, 24 Jan 2022 10:57:09 +0530
4Subject: [PATCH] tst-realpath-toolong: Fix hurd build
5
6Define PATH_MAX to a constant if it isn't already defined, like in hurd.
7
8Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
9(cherry picked from commit 976db046bc3a3738f69255ae00b0a09b8e77fd9c)
10
11Upstream-Status: Backport [https://sourceware.org/git/?p=glibc.git;a=commit;h=8c8a71c85f2ed5cc90d08d82ce645513fc907cb6]
12CVE: CVE-2021-3999
13
14Signed-off-by: Pgowda <pgowda.cve@gmail.com>
15---
16 stdlib/tst-realpath-toolong.c | 4 ++++
17 1 file changed, 4 insertions(+)
18
19diff --git a/stdlib/tst-realpath-toolong.c b/stdlib/tst-realpath-toolong.c
20index 8bed772460..4388890294 100644
21--- a/stdlib/tst-realpath-toolong.c
22+++ b/stdlib/tst-realpath-toolong.c
23@@ -29,6 +29,10 @@
24
25 #define BASENAME "tst-realpath-toolong."
26
27+#ifndef PATH_MAX
28+# define PATH_MAX 1024
29+#endif
30+
31 int
32 do_test (void)
33 {
34--
352.27.0
36
diff --git a/meta/recipes-core/glibc/glibc/0002-CVE-2021-3999.patch b/meta/recipes-core/glibc/glibc/0002-CVE-2021-3999.patch
new file mode 100644
index 0000000000..ef3a504fdf
--- /dev/null
+++ b/meta/recipes-core/glibc/glibc/0002-CVE-2021-3999.patch
@@ -0,0 +1,357 @@
1From 472e799a5f2102bc0c3206dbd5a801765fceb39c Mon Sep 17 00:00:00 2001
2From: Siddhesh Poyarekar <siddhesh@sourceware.org>
3Date: Fri, 21 Jan 2022 23:32:56 +0530
4Subject: [PATCH] getcwd: Set errno to ERANGE for size == 1 (CVE-2021-3999)
5
6No valid path returned by getcwd would fit into 1 byte, so reject the
7size early and return NULL with errno set to ERANGE. This change is
8prompted by CVE-2021-3999, which describes a single byte buffer
9underflow and overflow when all of the following conditions are met:
10
11- The buffer size (i.e. the second argument of getcwd) is 1 byte
12- The current working directory is too long
13- '/' is also mounted on the current working directory
14
15Sequence of events:
16
17- In sysdeps/unix/sysv/linux/getcwd.c, the syscall returns ENAMETOOLONG
18 because the linux kernel checks for name length before it checks
19 buffer size
20
21- The code falls back to the generic getcwd in sysdeps/posix
22
23- In the generic func, the buf[0] is set to '\0' on line 250
24
25- this while loop on line 262 is bypassed:
26
27 while (!(thisdev == rootdev && thisino == rootino))
28
29 since the rootfs (/) is bind mounted onto the directory and the flow
30 goes on to line 449, where it puts a '/' in the byte before the
31 buffer.
32
33- Finally on line 458, it moves 2 bytes (the underflowed byte and the
34 '\0') to the buf[0] and buf[1], resulting in a 1 byte buffer overflow.
35
36- buf is returned on line 469 and errno is not set.
37
38This resolves BZ #28769.
39
40Reviewed-by: Andreas Schwab <schwab@linux-m68k.org>
41Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
42Signed-off-by: Qualys Security Advisory <qsa@qualys.com>
43Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
44(cherry picked from commit 23e0e8f5f1fb5ed150253d986ecccdc90c2dcd5e)
45
46Upstream-Status: Backport [https://sourceware.org/git/?p=glibc.git;a=commit;h=472e799a5f2102bc0c3206dbd5a801765fceb39c]
47CVE: CVE-2021-3999
48
49Signed-off-by: Pgowda <pgowda.cve@gmail.com>
50---
51 NEWS | 6 +
52 sysdeps/posix/getcwd.c | 7 +
53 sysdeps/unix/sysv/linux/Makefile | 7 +-
54 .../unix/sysv/linux/tst-getcwd-smallbuff.c | 241 ++++++++++++++++++
55 4 files changed, 260 insertions(+), 1 deletion(-)
56 create mode 100644 sysdeps/unix/sysv/linux/tst-getcwd-smallbuff.c
57
58diff --git a/NEWS b/NEWS
59index b4f81c2668..8d7467d2c1 100644
60--- a/NEWS
61+++ b/NEWS
62@@ -214,6 +214,12 @@ Security related changes:
63 function could result in a memory leak and potential access of
64 uninitialized memory. Reported by Qualys.
65
66+ CVE-2021-3999: Passing a buffer of size exactly 1 byte to the getcwd
67+ function may result in an off-by-one buffer underflow and overflow
68+ when the current working directory is longer than PATH_MAX and also
69+ corresponds to the / directory through an unprivileged mount
70+ namespace. Reported by Qualys.
71+
72 The following bugs are resolved with this release:
73
74 [4737] libc: fork is not async-signal-safe
75diff --git a/sysdeps/posix/getcwd.c b/sysdeps/posix/getcwd.c
76index 13680026ff..b6984a382c 100644
77--- a/sysdeps/posix/getcwd.c
78+++ b/sysdeps/posix/getcwd.c
79@@ -187,6 +187,13 @@ __getcwd_generic (char *buf, size_t size
80 size_t allocated = size;
81 size_t used;
82
83+ /* A size of 1 byte is never useful. */
84+ if (allocated == 1)
85+ {
86+ __set_errno (ERANGE);
87+ return NULL;
88+ }
89+
90 #if HAVE_MINIMALLY_WORKING_GETCWD
91 /* If AT_FDCWD is not defined, the algorithm below is O(N**2) and
92 this is much slower than the system getcwd (at least on
93diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
94index 76ad06361c..9380d3848d 100644
95--- a/sysdeps/unix/sysv/linux/Makefile
96+++ b/sysdeps/unix/sysv/linux/Makefile
97@@ -331,7 +331,12 @@ sysdep_routines += xstatconv internal_st
98
99 sysdep_headers += bits/fcntl-linux.h
100
101-tests += tst-fallocate tst-fallocate64 tst-o_path-locks
102+tests += \
103+ tst-fallocate \
104+ tst-fallocate64 \
105+ tst-getcwd-smallbuff \
106+ tst-o_path-locks \
107+# tests
108 endif
109
110 ifeq ($(subdir),elf)
111diff --git a/sysdeps/unix/sysv/linux/tst-getcwd-smallbuff.c b/sysdeps/unix/sysv/linux/tst-getcwd-smallbuff.c
112new file mode 100644
113index 0000000000..d460d6e766
114--- /dev/null
115+++ b/sysdeps/unix/sysv/linux/tst-getcwd-smallbuff.c
116@@ -0,0 +1,241 @@
117+/* Verify that getcwd returns ERANGE for size 1 byte and does not underflow
118+ buffer when the CWD is too long and is also a mount target of /. See bug
119+ #28769 or CVE-2021-3999 for more context.
120+ Copyright The GNU Toolchain Authors.
121+ This file is part of the GNU C Library.
122+
123+ The GNU C Library is free software; you can redistribute it and/or
124+ modify it under the terms of the GNU Lesser General Public
125+ License as published by the Free Software Foundation; either
126+ version 2.1 of the License, or (at your option) any later version.
127+
128+ The GNU C Library is distributed in the hope that it will be useful,
129+ but WITHOUT ANY WARRANTY; without even the implied warranty of
130+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
131+ Lesser General Public License for more details.
132+
133+ You should have received a copy of the GNU Lesser General Public
134+ License along with the GNU C Library; if not, see
135+ <https://www.gnu.org/licenses/>. */
136+
137+#include <errno.h>
138+#include <fcntl.h>
139+#include <intprops.h>
140+#include <limits.h>
141+#include <stdio.h>
142+#include <stdlib.h>
143+#include <string.h>
144+#include <sys/mount.h>
145+#include <sys/stat.h>
146+#include <sys/types.h>
147+#include <sys/wait.h>
148+
149+#include <sys/socket.h>
150+#include <sys/un.h>
151+#include <support/check.h>
152+#include <support/temp_file.h>
153+#include <support/xsched.h>
154+#include <support/xunistd.h>
155+
156+static char *base;
157+#define BASENAME "tst-getcwd-smallbuff"
158+#define MOUNT_NAME "mpoint"
159+static int sockfd[2];
160+
161+static void
162+do_cleanup (void)
163+{
164+ support_chdir_toolong_temp_directory (base);
165+ TEST_VERIFY_EXIT (rmdir (MOUNT_NAME) == 0);
166+ free (base);
167+}
168+
169+static void
170+send_fd (const int sock, const int fd)
171+{
172+ struct msghdr msg = {0};
173+ union
174+ {
175+ struct cmsghdr hdr;
176+ char buf[CMSG_SPACE (sizeof (int))];
177+ } cmsgbuf = {0};
178+ struct cmsghdr *cmsg;
179+ struct iovec vec;
180+ char ch = 'A';
181+ ssize_t n;
182+
183+ msg.msg_control = &cmsgbuf.buf;
184+ msg.msg_controllen = sizeof (cmsgbuf.buf);
185+
186+ cmsg = CMSG_FIRSTHDR (&msg);
187+ cmsg->cmsg_len = CMSG_LEN (sizeof (int));
188+ cmsg->cmsg_level = SOL_SOCKET;
189+ cmsg->cmsg_type = SCM_RIGHTS;
190+ memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
191+
192+ vec.iov_base = &ch;
193+ vec.iov_len = 1;
194+ msg.msg_iov = &vec;
195+ msg.msg_iovlen = 1;
196+
197+ while ((n = sendmsg (sock, &msg, 0)) == -1 && errno == EINTR);
198+
199+ TEST_VERIFY_EXIT (n == 1);
200+}
201+
202+static int
203+recv_fd (const int sock)
204+{
205+ struct msghdr msg = {0};
206+ union
207+ {
208+ struct cmsghdr hdr;
209+ char buf[CMSG_SPACE(sizeof(int))];
210+ } cmsgbuf = {0};
211+ struct cmsghdr *cmsg;
212+ struct iovec vec;
213+ ssize_t n;
214+ char ch = '\0';
215+ int fd = -1;
216+
217+ vec.iov_base = &ch;
218+ vec.iov_len = 1;
219+ msg.msg_iov = &vec;
220+ msg.msg_iovlen = 1;
221+
222+ msg.msg_control = &cmsgbuf.buf;
223+ msg.msg_controllen = sizeof (cmsgbuf.buf);
224+
225+ while ((n = recvmsg (sock, &msg, 0)) == -1 && errno == EINTR);
226+ if (n != 1 || ch != 'A')
227+ return -1;
228+
229+ cmsg = CMSG_FIRSTHDR (&msg);
230+ if (cmsg == NULL)
231+ return -1;
232+ if (cmsg->cmsg_type != SCM_RIGHTS)
233+ return -1;
234+ memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
235+ if (fd < 0)
236+ return -1;
237+ return fd;
238+}
239+
240+static int
241+child_func (void * const arg)
242+{
243+ xclose (sockfd[0]);
244+ const int sock = sockfd[1];
245+ char ch;
246+
247+ TEST_VERIFY_EXIT (read (sock, &ch, 1) == 1);
248+ TEST_VERIFY_EXIT (ch == '1');
249+
250+ if (mount ("/", MOUNT_NAME, NULL, MS_BIND | MS_REC, NULL))
251+ FAIL_EXIT1 ("mount failed: %m\n");
252+ const int fd = xopen ("mpoint",
253+ O_RDONLY | O_PATH | O_DIRECTORY | O_NOFOLLOW, 0);
254+
255+ send_fd (sock, fd);
256+ xclose (fd);
257+
258+ TEST_VERIFY_EXIT (read (sock, &ch, 1) == 1);
259+ TEST_VERIFY_EXIT (ch == 'a');
260+
261+ xclose (sock);
262+ return 0;
263+}
264+
265+static void
266+update_map (char * const mapping, const char * const map_file)
267+{
268+ const size_t map_len = strlen (mapping);
269+
270+ const int fd = xopen (map_file, O_WRONLY, 0);
271+ xwrite (fd, mapping, map_len);
272+ xclose (fd);
273+}
274+
275+static void
276+proc_setgroups_write (const long child_pid, const char * const str)
277+{
278+ const size_t str_len = strlen(str);
279+
280+ char setgroups_path[sizeof ("/proc//setgroups") + INT_STRLEN_BOUND (long)];
281+
282+ snprintf (setgroups_path, sizeof (setgroups_path),
283+ "/proc/%ld/setgroups", child_pid);
284+
285+ const int fd = open (setgroups_path, O_WRONLY);
286+
287+ if (fd < 0)
288+ {
289+ TEST_VERIFY_EXIT (errno == ENOENT);
290+ FAIL_UNSUPPORTED ("/proc/%ld/setgroups not found\n", child_pid);
291+ }
292+
293+ xwrite (fd, str, str_len);
294+ xclose(fd);
295+}
296+
297+static char child_stack[1024 * 1024];
298+
299+int
300+do_test (void)
301+{
302+ base = support_create_and_chdir_toolong_temp_directory (BASENAME);
303+
304+ xmkdir (MOUNT_NAME, S_IRWXU);
305+ atexit (do_cleanup);
306+
307+ TEST_VERIFY_EXIT (socketpair (AF_UNIX, SOCK_STREAM, 0, sockfd) == 0);
308+ pid_t child_pid = xclone (child_func, NULL, child_stack,
309+ sizeof (child_stack),
310+ CLONE_NEWUSER | CLONE_NEWNS | SIGCHLD);
311+
312+ xclose (sockfd[1]);
313+ const int sock = sockfd[0];
314+
315+ char map_path[sizeof ("/proc//uid_map") + INT_STRLEN_BOUND (long)];
316+ char map_buf[sizeof ("0 1") + INT_STRLEN_BOUND (long)];
317+
318+ snprintf (map_path, sizeof (map_path), "/proc/%ld/uid_map",
319+ (long) child_pid);
320+ snprintf (map_buf, sizeof (map_buf), "0 %ld 1", (long) getuid());
321+ update_map (map_buf, map_path);
322+
323+ proc_setgroups_write ((long) child_pid, "deny");
324+ snprintf (map_path, sizeof (map_path), "/proc/%ld/gid_map",
325+ (long) child_pid);
326+ snprintf (map_buf, sizeof (map_buf), "0 %ld 1", (long) getgid());
327+ update_map (map_buf, map_path);
328+
329+ TEST_VERIFY_EXIT (send (sock, "1", 1, MSG_NOSIGNAL) == 1);
330+ const int fd = recv_fd (sock);
331+ TEST_VERIFY_EXIT (fd >= 0);
332+ TEST_VERIFY_EXIT (fchdir (fd) == 0);
333+
334+ static char buf[2 * 10 + 1];
335+ memset (buf, 'A', sizeof (buf));
336+
337+ /* Finally, call getcwd and check if it resulted in a buffer underflow. */
338+ char * cwd = getcwd (buf + sizeof (buf) / 2, 1);
339+ TEST_VERIFY (cwd == NULL);
340+ TEST_VERIFY (errno == ERANGE);
341+
342+ for (int i = 0; i < sizeof (buf); i++)
343+ if (buf[i] != 'A')
344+ {
345+ printf ("buf[%d] = %02x\n", i, (unsigned int) buf[i]);
346+ support_record_failure ();
347+ }
348+
349+ TEST_VERIFY_EXIT (send (sock, "a", 1, MSG_NOSIGNAL) == 1);
350+ xclose (sock);
351+ TEST_VERIFY_EXIT (xwaitpid (child_pid, NULL, 0) == child_pid);
352+
353+ return 0;
354+}
355+
356+#define CLEANUP_HANDLER do_cleanup
357+#include <support/test-driver.c>