1 | /* Testcase for BZ 30932. |
2 | Copyright (C) 2023-2024 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <setjmp.h> |
21 | #include <stdbool.h> |
22 | #include <stdio.h> |
23 | #include <string.h> |
24 | #include <sys/resource.h> |
25 | #include <unistd.h> |
26 | |
27 | #include <support/check.h> |
28 | #include <support/support.h> |
29 | #include <support/temp_file.h> |
30 | |
31 | jmp_buf chk_fail_buf; |
32 | bool chk_fail_ok; |
33 | |
34 | const char *str2 = "F" ; |
35 | char buf2[10] = "%s" ; |
36 | |
37 | static int |
38 | do_test (void) |
39 | { |
40 | struct rlimit rl; |
41 | int max_fd = 24; |
42 | |
43 | if (getrlimit (RLIMIT_NOFILE, rlimits: &rl) == -1) |
44 | FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m" ); |
45 | |
46 | max_fd = (rl.rlim_cur < max_fd ? rl.rlim_cur : max_fd); |
47 | rl.rlim_cur = max_fd; |
48 | |
49 | if (setrlimit (RLIMIT_NOFILE, rlimits: &rl) == 1) |
50 | FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m" ); |
51 | |
52 | /* Exhaust the file descriptor limit with temporary files. */ |
53 | int nfiles = 0; |
54 | for (; nfiles < max_fd; nfiles++) |
55 | { |
56 | int fd = create_temp_file (base: "tst-sprintf-fortify-rdonly-." , NULL); |
57 | if (fd == -1) |
58 | { |
59 | if (errno != EMFILE) |
60 | FAIL_EXIT1 ("create_temp_file: %m" ); |
61 | break; |
62 | } |
63 | } |
64 | TEST_VERIFY_EXIT (nfiles != 0); |
65 | |
66 | /* When the format string is writable and contains %n, |
67 | with -D_FORTIFY_SOURCE=2 it causes __chk_fail. However, if libc can not |
68 | open procfs to check if the input format string in within a writable |
69 | memory segment, the fortify version can not perform the check. */ |
70 | char buf[128]; |
71 | int n1; |
72 | int n2; |
73 | |
74 | strcpy (buf2 + 2, "%n%s%n" ); |
75 | if (sprintf (buf, buf2, str2, &n1, str2, &n2) != 2 |
76 | || n1 != 1 || n2 != 2) |
77 | FAIL_EXIT1 ("sprintf failed: %s %d %d" , buf, n1, n2); |
78 | |
79 | return 0; |
80 | } |
81 | |
82 | #include <support/test-driver.c> |
83 | |