• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *   http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <sys/capability.h>
18 #include "test.h"
19 
20 /**
21  * @tc.name      : capset_0100
22  * @tc.desc      : Set capabilities of thread
23  * @tc.level     : Level 0
24  */
capset_0100(void)25 void capset_0100(void)
26 {
27     struct __user_cap_header_struct cap_header;
28     struct __user_cap_data_struct cap_data;
29 
30     cap_header.pid = getpid();
31     cap_header.version = _LINUX_CAPABILITY_VERSION_1;
32 
33     if (capget(&cap_header, &cap_data) < 0) {
34         t_error("%s capget failed\n", __func__);
35     }
36 
37     printf("capheader: %x  %d\n", cap_header.version, cap_header.pid);
38     printf("capdata: %x  %x  %x\n", cap_data.effective, cap_data.permitted, cap_data.inheritable);
39 
40     __u32 cap_mask = 0;
41     cap_mask |= (1 << CAP_NET_BIND_SERVICE);
42     cap_data.effective = cap_mask;
43     cap_data.permitted = cap_mask;
44     cap_data.inheritable = 0;
45 
46     if (capset(&cap_header, &cap_data) < 0) {
47         t_error("%s capset failed\n", __func__);
48     }
49     printf("%d\n", capget(&cap_header, &cap_data));
50     printf("capheader: %x  %d\n", cap_header.version, cap_header.pid);
51     printf("capdata: %x  %x  %x\n", cap_data.effective, cap_data.permitted, cap_data.inheritable);
52 }
53 
main(int argc,char * argv[])54 int main(int argc, char *argv[])
55 {
56     capset_0100();
57     return t_status;
58 }