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