1 /*
2 * Copyright (c) 2022-2023 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 "token_setproc.h"
17
18 #include <errno.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <sys/ioctl.h>
23 #include <unistd.h>
24
25 #define ACCESS_TOKENID_GET_TOKENID \
26 _IOR(ACCESS_TOKEN_ID_IOCTL_BASE, GET_TOKEN_ID, uint64_t)
27 #define ACCESS_TOKENID_SET_TOKENID \
28 _IOW(ACCESS_TOKEN_ID_IOCTL_BASE, SET_TOKEN_ID, uint64_t)
29 #define ACCESS_TOKENID_GET_FTOKENID \
30 _IOR(ACCESS_TOKEN_ID_IOCTL_BASE, GET_FTOKEN_ID, uint64_t)
31 #define ACCESS_TOKENID_SET_FTOKENID \
32 _IOW(ACCESS_TOKEN_ID_IOCTL_BASE, SET_FTOKEN_ID, uint64_t)
33
34 #define INVAL_TOKEN_ID 0x0
35 #define TOKEN_ID_LOWMASK 0xffffffff
36
37 const uint64_t SET_PROC_FD_TAG = 0xD005A01;
38
GetSelfTokenID(void)39 uint64_t GetSelfTokenID(void)
40 {
41 uint64_t token = INVAL_TOKEN_ID;
42 int fd = open(TOKENID_DEVNODE, O_RDWR);
43 if (fd < 0) {
44 return INVAL_TOKEN_ID;
45 }
46 fdsan_exchange_owner_tag(fd, 0, SET_PROC_FD_TAG);
47 int ret = ioctl(fd, ACCESS_TOKENID_GET_TOKENID, &token);
48 if (ret) {
49 (void)fdsan_close_with_tag(fd, SET_PROC_FD_TAG);
50 return INVAL_TOKEN_ID;
51 }
52
53 (void)fdsan_close_with_tag(fd, SET_PROC_FD_TAG);
54 return token;
55 }
56
SetSelfTokenID(uint64_t tokenID)57 int SetSelfTokenID(uint64_t tokenID)
58 {
59 int fd = open(TOKENID_DEVNODE, O_RDWR);
60 if (fd < 0) {
61 return ACCESS_TOKEN_OPEN_ERROR;
62 }
63 fdsan_exchange_owner_tag(fd, 0, SET_PROC_FD_TAG);
64 int ret = ioctl(fd, ACCESS_TOKENID_SET_TOKENID, &tokenID);
65 if (ret) {
66 (void)fdsan_close_with_tag(fd, SET_PROC_FD_TAG);
67 return ret;
68 }
69
70 (void)fdsan_close_with_tag(fd, SET_PROC_FD_TAG);
71 return ACCESS_TOKEN_OK;
72 }
73
GetFirstCallerTokenID(void)74 uint64_t GetFirstCallerTokenID(void)
75 {
76 uint64_t token = INVAL_TOKEN_ID;
77 int fd = open(TOKENID_DEVNODE, O_RDWR);
78 if (fd < 0) {
79 return INVAL_TOKEN_ID;
80 }
81 fdsan_exchange_owner_tag(fd, 0, SET_PROC_FD_TAG);
82 int ret = ioctl(fd, ACCESS_TOKENID_GET_FTOKENID, &token);
83 if (ret) {
84 (void)fdsan_close_with_tag(fd, SET_PROC_FD_TAG);
85 return INVAL_TOKEN_ID;
86 }
87
88 (void)fdsan_close_with_tag(fd, SET_PROC_FD_TAG);
89 return token;
90 }
91
SetFirstCallerTokenID(uint64_t tokenID)92 int SetFirstCallerTokenID(uint64_t tokenID)
93 {
94 int fd = open(TOKENID_DEVNODE, O_RDWR);
95 if (fd < 0) {
96 return ACCESS_TOKEN_OPEN_ERROR;
97 }
98 fdsan_exchange_owner_tag(fd, 0, SET_PROC_FD_TAG);
99 int ret = ioctl(fd, ACCESS_TOKENID_SET_FTOKENID, &tokenID);
100 if (ret) {
101 (void)fdsan_close_with_tag(fd, SET_PROC_FD_TAG);
102 return ret;
103 }
104
105 (void)fdsan_close_with_tag(fd, SET_PROC_FD_TAG);
106 return ACCESS_TOKEN_OK;
107 }
108