1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2017 Fujitsu Ltd.
4 * Author: Xiao Yang <[email protected]>
5 */
6
7 /*
8 * Description:
9 * fcntl(2) manpage states that an unprivileged user could not set the
10 * pipe capacity above the limit in /proc/sys/fs/pipe-max-size. However,
11 * an unprivileged user could create a pipe whose initial capacity exceeds
12 * the limit. We add a regression test to check that pipe-max-size caps
13 * the initial allocation for a new pipe for unprivileged users, but not
14 * for privileged users.
15 *
16 * This kernel bug has been fixed by:
17 *
18 * commit 086e774a57fba4695f14383c0818994c0b31da7c
19 * Author: Michael Kerrisk (man-pages) <[email protected]>
20 * Date: Tue Oct 11 13:53:43 2016 -0700
21 *
22 * pipe: cap initial pipe capacity according to pipe-max-size limit
23 */
24
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <pwd.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30
31 #include "lapi/fcntl.h"
32 #include "tst_test.h"
33 #include "pgsize_helpers.h"
34
35 #define PIPE_DEF_BUFFERS 16
36
37 static int pipe_max_unpriv;
38 static int test_max_unpriv;
39 static int test_max_priv;
40 static struct passwd *pw;
41 static struct tcase {
42 int *exp_sz;
43 int exp_usr;
44 char *des;
45 } tcases[] = {
46 {&test_max_unpriv, 1, "an unprivileged user"},
47 {&test_max_priv, 0, "a privileged user"}
48 };
49
setup(void)50 static void setup(void)
51 {
52 test_max_unpriv = getpagesize();
53 /*
54 * The test doesn't set the pipe's size so we are actually checking the default size.
55 * In the case of a privileged user this is determined by PIPE_DEF_BUFFERS number of
56 * pages in the kernel.
57 */
58 test_max_priv = kernel_page_size() * PIPE_DEF_BUFFERS;
59
60 if (!access("/proc/sys/fs/pipe-max-size", F_OK)) {
61 SAFE_FILE_SCANF("/proc/sys/fs/pipe-max-size", "%d",
62 &pipe_max_unpriv);
63 SAFE_FILE_PRINTF("/proc/sys/fs/pipe-max-size", "%d",
64 test_max_unpriv);
65 } else {
66 tst_brk(TCONF, "/proc/sys/fs/pipe-max-size doesn't exist");
67 }
68
69 pw = SAFE_GETPWNAM("nobody");
70 }
71
cleanup(void)72 static void cleanup(void)
73 {
74 SAFE_FILE_PRINTF("/proc/sys/fs/pipe-max-size", "%d", pipe_max_unpriv);
75 }
76
verify_pipe_size(int exp_pip_sz,char * desp)77 static int verify_pipe_size(int exp_pip_sz, char *desp)
78 {
79 int get_size;
80 int fds[2];
81
82 SAFE_PIPE(fds);
83
84 get_size = fcntl(fds[1], F_GETPIPE_SZ);
85 if (get_size == -1) {
86 tst_res(TFAIL | TERRNO, "fcntl(2) with F_GETPIPE_SZ failed");
87 goto end;
88 }
89
90 if (get_size != exp_pip_sz) {
91 tst_res(TFAIL, "%s init the capacity of a pipe to %d "
92 "unexpectedly, expected %d", desp, get_size,
93 exp_pip_sz);
94 } else {
95 tst_res(TPASS, "%s init the capacity of a pipe to %d "
96 "successfully", desp, exp_pip_sz);
97 }
98
99 end:
100 if (fds[0] > 0)
101 SAFE_CLOSE(fds[0]);
102
103 if (fds[1] > 0)
104 SAFE_CLOSE(fds[1]);
105
106 exit(0);
107 }
108
do_test(unsigned int n)109 static void do_test(unsigned int n)
110 {
111 struct tcase *tc = &tcases[n];
112
113 if (!SAFE_FORK()) {
114 if (tc->exp_usr)
115 SAFE_SETUID(pw->pw_uid);
116
117 verify_pipe_size(*tc->exp_sz, tc->des);
118 }
119
120 tst_reap_children();
121 }
122
123 static struct tst_test test = {
124 .needs_root = 1,
125 .forks_child = 1,
126 .tcnt = ARRAY_SIZE(tcases),
127 .setup = setup,
128 .cleanup = cleanup,
129 .test = do_test,
130 .tags = (const struct tst_tag[]) {
131 {"linux-git", "086e774a57fb"},
132 {}
133 }
134 };
135