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 6*49cdfc7eSAndroid Build Coastguard Worker /** 7*49cdfc7eSAndroid Build Coastguard Worker * DOC: Capabilities introduction 8*49cdfc7eSAndroid Build Coastguard Worker * 9*49cdfc7eSAndroid Build Coastguard Worker * Limited capability operations without libcap. 10*49cdfc7eSAndroid Build Coastguard Worker */ 11*49cdfc7eSAndroid Build Coastguard Worker 12*49cdfc7eSAndroid Build Coastguard Worker #ifndef TST_CAPABILITY_H 13*49cdfc7eSAndroid Build Coastguard Worker #define TST_CAPABILITY_H 14*49cdfc7eSAndroid Build Coastguard Worker 15*49cdfc7eSAndroid Build Coastguard Worker #include <stdint.h> 16*49cdfc7eSAndroid Build Coastguard Worker 17*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/capability.h" 18*49cdfc7eSAndroid Build Coastguard Worker 19*49cdfc7eSAndroid Build Coastguard Worker /** 20*49cdfc7eSAndroid Build Coastguard Worker * enum tst_cap_act - A capability action masks. 21*49cdfc7eSAndroid Build Coastguard Worker * 22*49cdfc7eSAndroid Build Coastguard Worker * @TST_CAP_DROP: Drop capabilities. 23*49cdfc7eSAndroid Build Coastguard Worker * @TST_CAP_REQ: Add capabilities. 24*49cdfc7eSAndroid Build Coastguard Worker */ 25*49cdfc7eSAndroid Build Coastguard Worker enum tst_cap_act { 26*49cdfc7eSAndroid Build Coastguard Worker TST_CAP_DROP = 1, 27*49cdfc7eSAndroid Build Coastguard Worker TST_CAP_REQ = (1 << 1) 28*49cdfc7eSAndroid Build Coastguard Worker }; 29*49cdfc7eSAndroid Build Coastguard Worker 30*49cdfc7eSAndroid Build Coastguard Worker /** 31*49cdfc7eSAndroid Build Coastguard Worker * struct tst_cap_user_header - Kernel capget(), capset() syscall header. 32*49cdfc7eSAndroid Build Coastguard Worker * 33*49cdfc7eSAndroid Build Coastguard Worker * @version: A capability API version. 34*49cdfc7eSAndroid Build Coastguard Worker * @pid: A process to operate on. 35*49cdfc7eSAndroid Build Coastguard Worker */ 36*49cdfc7eSAndroid Build Coastguard Worker struct tst_cap_user_header { 37*49cdfc7eSAndroid Build Coastguard Worker uint32_t version; 38*49cdfc7eSAndroid Build Coastguard Worker int pid; 39*49cdfc7eSAndroid Build Coastguard Worker }; 40*49cdfc7eSAndroid Build Coastguard Worker 41*49cdfc7eSAndroid Build Coastguard Worker /** 42*49cdfc7eSAndroid Build Coastguard Worker * struct tst_cap_user_data - Kernel capset(), capget() syscall payload. 43*49cdfc7eSAndroid Build Coastguard Worker * 44*49cdfc7eSAndroid Build Coastguard Worker * @effective: A capability effective set. 45*49cdfc7eSAndroid Build Coastguard Worker * @permitted: A capability permitted set. 46*49cdfc7eSAndroid Build Coastguard Worker * @inheritable: A capability inheritable set. 47*49cdfc7eSAndroid Build Coastguard Worker */ 48*49cdfc7eSAndroid Build Coastguard Worker struct tst_cap_user_data { 49*49cdfc7eSAndroid Build Coastguard Worker uint32_t effective; 50*49cdfc7eSAndroid Build Coastguard Worker uint32_t permitted; 51*49cdfc7eSAndroid Build Coastguard Worker uint32_t inheritable; 52*49cdfc7eSAndroid Build Coastguard Worker }; 53*49cdfc7eSAndroid Build Coastguard Worker 54*49cdfc7eSAndroid Build Coastguard Worker /** 55*49cdfc7eSAndroid Build Coastguard Worker * struct tst_cap - A capability to alter. 56*49cdfc7eSAndroid Build Coastguard Worker * 57*49cdfc7eSAndroid Build Coastguard Worker * @action: What should we do, i.e. drop or add a capability. 58*49cdfc7eSAndroid Build Coastguard Worker * @id: A capability id. 59*49cdfc7eSAndroid Build Coastguard Worker * @name: A capability name. 60*49cdfc7eSAndroid Build Coastguard Worker * 61*49cdfc7eSAndroid Build Coastguard Worker * This structure is usually constructed with the TST_CAP() macro so that the 62*49cdfc7eSAndroid Build Coastguard Worker * name is created automatically. 63*49cdfc7eSAndroid Build Coastguard Worker */ 64*49cdfc7eSAndroid Build Coastguard Worker struct tst_cap { 65*49cdfc7eSAndroid Build Coastguard Worker uint32_t action; 66*49cdfc7eSAndroid Build Coastguard Worker uint32_t id; 67*49cdfc7eSAndroid Build Coastguard Worker char *name; 68*49cdfc7eSAndroid Build Coastguard Worker }; 69*49cdfc7eSAndroid Build Coastguard Worker 70*49cdfc7eSAndroid Build Coastguard Worker /** 71*49cdfc7eSAndroid Build Coastguard Worker * TST_CAP() - Create a struct tst_cap entry. 72*49cdfc7eSAndroid Build Coastguard Worker * 73*49cdfc7eSAndroid Build Coastguard Worker * @action: What should we do, i.e. drop or add capability. 74*49cdfc7eSAndroid Build Coastguard Worker * @capability: A capability id, e.g. CAP_BPF. 75*49cdfc7eSAndroid Build Coastguard Worker */ 76*49cdfc7eSAndroid Build Coastguard Worker #define TST_CAP(action, capability) {action, capability, #capability} 77*49cdfc7eSAndroid Build Coastguard Worker 78*49cdfc7eSAndroid Build Coastguard Worker /** 79*49cdfc7eSAndroid Build Coastguard Worker * tst_capget() - Get the capabilities as decided by hdr. 80*49cdfc7eSAndroid Build Coastguard Worker * 81*49cdfc7eSAndroid Build Coastguard Worker * @hdr: A capability user header stores a pid to operate on and which 82*49cdfc7eSAndroid Build Coastguard Worker * capability API version is used. 83*49cdfc7eSAndroid Build Coastguard Worker * @data: A memory to store the capabilities to. The memory pointed to by data 84*49cdfc7eSAndroid Build Coastguard Worker * should be large enough to store two structs. 85*49cdfc7eSAndroid Build Coastguard Worker * 86*49cdfc7eSAndroid Build Coastguard Worker * return: Returns 0 on success, -1 on a failure and sets errno. 87*49cdfc7eSAndroid Build Coastguard Worker */ 88*49cdfc7eSAndroid Build Coastguard Worker int tst_capget(struct tst_cap_user_header *hdr, 89*49cdfc7eSAndroid Build Coastguard Worker struct tst_cap_user_data *data); 90*49cdfc7eSAndroid Build Coastguard Worker 91*49cdfc7eSAndroid Build Coastguard Worker /** 92*49cdfc7eSAndroid Build Coastguard Worker * tst_capset() - Set the capabilities as decided by hdr and data 93*49cdfc7eSAndroid Build Coastguard Worker * 94*49cdfc7eSAndroid Build Coastguard Worker * @hdr: A capability user header stores a pid to operate on and which 95*49cdfc7eSAndroid Build Coastguard Worker * capability API version is used. 96*49cdfc7eSAndroid Build Coastguard Worker * @data: A memory to store the capabilities to. The memory pointed to by data 97*49cdfc7eSAndroid Build Coastguard Worker * should be large enough to store two structs. 98*49cdfc7eSAndroid Build Coastguard Worker * 99*49cdfc7eSAndroid Build Coastguard Worker * return: Returns 0 on success, -1 on a failure and sets errno. 100*49cdfc7eSAndroid Build Coastguard Worker */ 101*49cdfc7eSAndroid Build Coastguard Worker int tst_capset(struct tst_cap_user_header *hdr, 102*49cdfc7eSAndroid Build Coastguard Worker const struct tst_cap_user_data *data); 103*49cdfc7eSAndroid Build Coastguard Worker 104*49cdfc7eSAndroid Build Coastguard Worker /** 105*49cdfc7eSAndroid Build Coastguard Worker * tst_cap_action() - Add, check or remove a capability. 106*49cdfc7eSAndroid Build Coastguard Worker * 107*49cdfc7eSAndroid Build Coastguard Worker * @cap: An {} terminated array of capabilities to alter. 108*49cdfc7eSAndroid Build Coastguard Worker * 109*49cdfc7eSAndroid Build Coastguard Worker * It will attempt to drop or add capability to the effective set. It will 110*49cdfc7eSAndroid Build Coastguard Worker * try to detect if this is needed and whether it can or can't be done. If it 111*49cdfc7eSAndroid Build Coastguard Worker * clearly can not add a privilege to the effective set then it will return 112*49cdfc7eSAndroid Build Coastguard Worker * TCONF. However it may fail for some other reason and return TBROK. 113*49cdfc7eSAndroid Build Coastguard Worker * 114*49cdfc7eSAndroid Build Coastguard Worker * This only tries to change the effective set. Some tests may need to change 115*49cdfc7eSAndroid Build Coastguard Worker * the inheritable and ambient sets, so that child processes retain some 116*49cdfc7eSAndroid Build Coastguard Worker * capability. 117*49cdfc7eSAndroid Build Coastguard Worker */ 118*49cdfc7eSAndroid Build Coastguard Worker void tst_cap_action(struct tst_cap *cap); 119*49cdfc7eSAndroid Build Coastguard Worker 120*49cdfc7eSAndroid Build Coastguard Worker 121*49cdfc7eSAndroid Build Coastguard Worker /** 122*49cdfc7eSAndroid Build Coastguard Worker * tst_cap_setup() - Add, check or remove a capabilities. 123*49cdfc7eSAndroid Build Coastguard Worker * 124*49cdfc7eSAndroid Build Coastguard Worker * @cap: An {} terminated array of capabilities to alter. 125*49cdfc7eSAndroid Build Coastguard Worker * @action_mask: Decides which actions are done, i.e. only drop caps, add them 126*49cdfc7eSAndroid Build Coastguard Worker * or both. 127*49cdfc7eSAndroid Build Coastguard Worker * 128*49cdfc7eSAndroid Build Coastguard Worker * Takes a NULL terminated array of structs which describe whether some 129*49cdfc7eSAndroid Build Coastguard Worker * capabilities are needed or not and mask that determines subset of the 130*49cdfc7eSAndroid Build Coastguard Worker * actions to be performed. Loops over the array and if mask matches the 131*49cdfc7eSAndroid Build Coastguard Worker * element action it's passed to tst_cap_action(). 132*49cdfc7eSAndroid Build Coastguard Worker */ 133*49cdfc7eSAndroid Build Coastguard Worker void tst_cap_setup(struct tst_cap *cap, enum tst_cap_act action_mask); 134*49cdfc7eSAndroid Build Coastguard Worker 135*49cdfc7eSAndroid Build Coastguard Worker #endif /* TST_CAPABILITY_H */ 136