1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
4  * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
5  */
6 #ifndef PRCTL06_H
7 #define PRCTL06_H
8 
9 #include <errno.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <sys/prctl.h>
13 #include <pwd.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <lapi/prctl.h>
17 #include "tst_test.h"
18 
19 #define PROC_STATUS        "/proc/self/status"
20 #define IPC_ENV_VAR        "LTP_IPC_PATH"
21 #define MNTPOINT           "mntpoint"
22 #define TESTBIN            "prctl06_execve"
23 #define TEST_REL_BIN_DIR   MNTPOINT"/"
24 #define BIN_PATH           MNTPOINT"/"TESTBIN
25 #define SUID_MODE          (S_ISUID|S_ISGID|S_IXUSR|S_IXGRP|S_IXOTH)
26 
check_proc_field(int val,char * name)27 void check_proc_field(int val, char *name)
28 {
29 	static int flag = 1;
30 	int field = 0;
31 
32 	if (!flag)
33 		return;
34 
35 	TEST(FILE_LINES_SCANF(PROC_STATUS, "NoNewPrivs:%d", &field));
36 	if (TST_RET == 1) {
37 		tst_res(TCONF,
38 			"%s doesn't support NoNewPrivs field", PROC_STATUS);
39 		flag = 0;
40 		return;
41 	}
42 	if (val == field)
43 		tst_res(TPASS, "%s %s NoNewPrivs field expected %d got %d",
44 			name, PROC_STATUS, val, field);
45 	else
46 		tst_res(TFAIL, "%s %s NoNewPrivs field expected %d got %d",
47 			name, PROC_STATUS, val, field);
48 }
49 
check_no_new_privs(int val,char * name)50 void check_no_new_privs(int val, char *name)
51 {
52 	TEST(prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0));
53 	if (TST_RET == val)
54 		tst_res(TPASS,
55 			"%s prctl(PR_GET_NO_NEW_PRIVS) expected %d got %d",
56 			name, val, val);
57 	else
58 		tst_res(TFAIL,
59 			"%s prctl(PR_GET_NO_NEW_PRIVS) expected %d got %ld",
60 			name, val, TST_RET);
61 
62 	check_proc_field(val, name);
63 }
64 
65 #endif
66