Home | History | Annotate | Download | only in fcntl
      1 /*
      2  * Copyright (c) 2017 Fujitsu Ltd.
      3  * Author: Xiao Yang <yangx.jy (at) cn.fujitsu.com>
      4  *
      5  * This program is free software: you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation, either version 2 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This program 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
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with this program. If not, see <http://www.gnu.org/licenses/>.*
     17  */
     18 
     19 /*
     20  * Description:
     21  * fcntl(2) manpage states that an unprivileged user could not set the
     22  * pipe capacity above the limit in /proc/sys/fs/pipe-max-size.  However,
     23  * an unprivileged user could create a pipe whose initial capacity exceeds
     24  * the limit.  We add a regression test to check that pipe-max-size caps
     25  * the initial allocation for a new pipe for unprivileged users, but not
     26  * for privileged users.
     27  *
     28  * This kernel bug has been fixed by:
     29  *
     30  * commit 086e774a57fba4695f14383c0818994c0b31da7c
     31  * Author: Michael Kerrisk (man-pages) <mtk.manpages (at) gmail.com>
     32  * Date:   Tue Oct 11 13:53:43 2016 -0700
     33  *
     34  * pipe: cap initial pipe capacity according to pipe-max-size limit
     35  */
     36 
     37 #include <errno.h>
     38 #include <sys/types.h>
     39 #include <pwd.h>
     40 #include <unistd.h>
     41 #include <fcntl.h>
     42 #include <stdlib.h>
     43 
     44 #include "lapi/fcntl.h"
     45 #include "tst_test.h"
     46 
     47 static int pipe_max_unpriv;
     48 static int test_max_unpriv;
     49 static int test_max_priv;
     50 static struct passwd *pw;
     51 static struct tcase {
     52 	int *exp_sz;
     53 	int exp_usr;
     54 	char *des;
     55 } tcases[] = {
     56 	{&test_max_unpriv, 1, "an unprivileged user"},
     57 	{&test_max_priv, 0, "a privileged user"}
     58 };
     59 
     60 static void setup(void)
     61 {
     62 	test_max_unpriv = getpagesize();
     63 	test_max_priv = test_max_unpriv * 16;
     64 
     65 	if (!access("/proc/sys/fs/pipe-max-size", F_OK)) {
     66 		SAFE_FILE_SCANF("/proc/sys/fs/pipe-max-size", "%d",
     67 				&pipe_max_unpriv);
     68 		SAFE_FILE_PRINTF("/proc/sys/fs/pipe-max-size", "%d",
     69 				test_max_unpriv);
     70 	} else {
     71 		tst_brk(TCONF, "/proc/sys/fs/pipe-max-size doesn't exist");
     72 	}
     73 
     74 	pw = SAFE_GETPWNAM("nobody");
     75 }
     76 
     77 static void cleanup(void)
     78 {
     79 	SAFE_FILE_PRINTF("/proc/sys/fs/pipe-max-size", "%d", pipe_max_unpriv);
     80 }
     81 
     82 static int verify_pipe_size(int exp_pip_sz, char *desp)
     83 {
     84 	int get_size;
     85 	int fds[2];
     86 
     87 	SAFE_PIPE(fds);
     88 
     89 	get_size = fcntl(fds[1], F_GETPIPE_SZ);
     90 	if (get_size == -1) {
     91 		tst_res(TFAIL | TERRNO, "fcntl(2) with F_GETPIPE_SZ failed");
     92 		goto end;
     93 	}
     94 
     95 	if (get_size != exp_pip_sz) {
     96 		tst_res(TFAIL, "%s init the capacity of a pipe to %d "
     97 			"unexpectedly, expected %d", desp, get_size,
     98 			exp_pip_sz);
     99 	} else {
    100 		tst_res(TPASS, "%s init the capacity of a pipe to %d "
    101 			"successfully", desp, exp_pip_sz);
    102 	}
    103 
    104 end:
    105 	if (fds[0] > 0)
    106 		SAFE_CLOSE(fds[0]);
    107 
    108 	if (fds[1] > 0)
    109 		SAFE_CLOSE(fds[1]);
    110 
    111 	exit(0);
    112 }
    113 
    114 static void do_test(unsigned int n)
    115 {
    116 	struct tcase *tc = &tcases[n];
    117 
    118 	if (!SAFE_FORK()) {
    119 		if (tc->exp_usr)
    120 			SAFE_SETUID(pw->pw_uid);
    121 
    122 		verify_pipe_size(*tc->exp_sz, tc->des);
    123 	}
    124 
    125 	tst_reap_children();
    126 }
    127 
    128 static struct tst_test test = {
    129 	.min_kver = "2.6.35",
    130 	.needs_root = 1,
    131 	.forks_child = 1,
    132 	.tcnt = ARRAY_SIZE(tcases),
    133 	.setup = setup,
    134 	.cleanup = cleanup,
    135 	.test = do_test
    136 };
    137