xref: /aosp_15_r20/external/ltp/lib/newlib_tests/tst_capability01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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) 2019 Richard Palethorpe <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * The user or file requires CAP_NET_RAW for this test to work.
6*49cdfc7eSAndroid Build Coastguard Worker  * e.g use "$ setcap cap_net_raw=pei tst_capability"
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
13*49cdfc7eSAndroid Build Coastguard Worker #include "tst_capability.h"
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_net.h"
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/socket.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
run(void)18*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
19*49cdfc7eSAndroid Build Coastguard Worker {
20*49cdfc7eSAndroid Build Coastguard Worker 	TEST(socket(AF_INET, SOCK_RAW, 1));
21*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET > -1) {
22*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Created raw socket");
23*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(TST_RET);
24*49cdfc7eSAndroid Build Coastguard Worker 	} else if (TST_ERR != EPERM) {
25*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
26*49cdfc7eSAndroid Build Coastguard Worker 			"Failed to create socket for wrong reason");
27*49cdfc7eSAndroid Build Coastguard Worker 	} else {
28*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "Didn't create raw socket");
29*49cdfc7eSAndroid Build Coastguard Worker 	}
30*49cdfc7eSAndroid Build Coastguard Worker }
31*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)32*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker 	if (geteuid() == 0)
35*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN, "CAP_NET_RAW may be ignored when euid == 0");
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	TEST(socket(AF_INET, SOCK_RAW, 1));
38*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET < 0)
39*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TFAIL | TTERRNO, "Can't create raw socket in setup");
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(TST_RET);
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
45*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
46*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
47*49cdfc7eSAndroid Build Coastguard Worker 	.caps = (struct tst_cap []) {
48*49cdfc7eSAndroid Build Coastguard Worker 		TST_CAP(TST_CAP_REQ, CAP_NET_RAW),
49*49cdfc7eSAndroid Build Coastguard Worker 		TST_CAP(TST_CAP_DROP, CAP_NET_RAW),
50*49cdfc7eSAndroid Build Coastguard Worker 		{}
51*49cdfc7eSAndroid Build Coastguard Worker 	},
52*49cdfc7eSAndroid Build Coastguard Worker };
53