• 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 #include "key_control.h"
16 
17 #include <bits/fcntl.h>
18 #include <ctype.h>
19 #include <sys/syscall.h>
20 #include <fcntl.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/ioctl.h>
26 #include <unistd.h>
27 #include <linux/keyctl.h>
28 
29 #include "fscrypt_log.h"
30 #include "fscrypt_sysparam.h"
31 #include "init_utils.h"
32 #include "securec.h"
33 
KeyCtrlGetKeyringId(key_serial_t id,int create)34 key_serial_t KeyCtrlGetKeyringId(key_serial_t id, int create)
35 {
36     return syscall(__NR_keyctl, KEYCTL_GET_KEYRING_ID, id, create);
37 }
38 
KeyCtrlAddKey(const char * type,const char * description,const key_serial_t ringId)39 key_serial_t KeyCtrlAddKey(const char *type, const char *description,
40     const key_serial_t ringId)
41 {
42     return syscall(__NR_add_key, type, description, NULL, 0, ringId);
43 }
44 
KeyCtrlAddKeyEx(const char * type,const char * description,struct fscrypt_key * fsKey,const key_serial_t ringId)45 key_serial_t KeyCtrlAddKeyEx(const char *type, const char *description,
46     struct fscrypt_key *fsKey, const key_serial_t ringId)
47 {
48     return syscall(__NR_add_key, type, description,
49         (void *)(fsKey), sizeof(struct fscrypt_key),
50         ringId);
51 }
52 
KeyCtrlSearch(key_serial_t ringId,const char * type,const char * description,key_serial_t destRingId)53 long KeyCtrlSearch(key_serial_t ringId, const char *type, const char *description,
54     key_serial_t destRingId)
55 {
56     return syscall(__NR_keyctl, KEYCTL_SEARCH, ringId, type,
57         description, destRingId);
58 }
59 
KeyCtrlUnlink(key_serial_t key,key_serial_t keyring)60 long KeyCtrlUnlink(key_serial_t key, key_serial_t keyring)
61 {
62     return syscall(__NR_keyctl, KEYCTL_UNLINK, key, keyring);
63 }
64 
FsIoctl(const char * mnt,unsigned long cmd,void * arg)65 static bool FsIoctl(const char *mnt, unsigned long cmd, void *arg)
66 {
67     int fd = open(mnt, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
68     if (fd < 0) {
69         FSCRYPT_LOGE("open %s failed, errno:%d", mnt, errno);
70         return false;
71     }
72     if (ioctl(fd, cmd, arg) != 0) {
73         FSCRYPT_LOGE("ioctl to %s failed, errno:%d", mnt, errno);
74         (void)close(fd);
75         return false;
76     }
77     (void)close(fd);
78     FSCRYPT_LOGI("success");
79     return true;
80 }
81 
82 #ifdef SUPPORT_FSCRYPT_V2
KeyCtrlInstallKey(const char * mnt,struct fscrypt_add_key_arg * arg)83 bool KeyCtrlInstallKey(const char *mnt, struct fscrypt_add_key_arg *arg)
84 {
85     FSCRYPT_LOGI("enter");
86     return FsIoctl(mnt, FS_IOC_ADD_ENCRYPTION_KEY, (void *)(arg));
87 }
88 
KeyCtrlRemoveKey(const char * mnt,struct fscrypt_remove_key_arg * arg)89 bool KeyCtrlRemoveKey(const char *mnt, struct fscrypt_remove_key_arg *arg)
90 {
91     FSCRYPT_LOGI("enter");
92     return FsIoctl(mnt, FS_IOC_REMOVE_ENCRYPTION_KEY, (void *)arg);
93 }
94 
KeyCtrlGetKeyStatus(const char * mnt,struct fscrypt_get_key_status_arg * arg)95 bool KeyCtrlGetKeyStatus(const char *mnt, struct fscrypt_get_key_status_arg *arg)
96 {
97     FSCRYPT_LOGI("enter");
98     return FsIoctl(mnt, FS_IOC_GET_ENCRYPTION_KEY_STATUS, (void *)(arg));
99 }
100 
KeyCtrlGetPolicyEx(const char * path,struct fscrypt_get_policy_ex_arg * policy)101 bool KeyCtrlGetPolicyEx(const char *path, struct fscrypt_get_policy_ex_arg *policy)
102 {
103     FSCRYPT_LOGI("enter");
104     return FsIoctl(path, FS_IOC_GET_ENCRYPTION_POLICY_EX, (void *)(policy));
105 }
106 #endif
107 
KeyCtrlSetPolicy(const char * path,union FscryptPolicy * policy)108 bool KeyCtrlSetPolicy(const char *path, union FscryptPolicy *policy)
109 {
110     FSCRYPT_LOGI("enter");
111     return FsIoctl(path, FS_IOC_SET_ENCRYPTION_POLICY, (void *)(policy));
112 }
113 
KeyCtrlGetPolicy(const char * path,struct fscrypt_policy * policy)114 bool KeyCtrlGetPolicy(const char *path, struct fscrypt_policy *policy)
115 {
116     FSCRYPT_LOGI("enter");
117     return FsIoctl(path, FS_IOC_GET_ENCRYPTION_POLICY, (void *)(policy));
118 }
119 
CheckKernelFscrypt(const char * mnt)120 static uint8_t CheckKernelFscrypt(const char *mnt)
121 {
122     int fd = open(mnt, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
123     if (fd < 0) {
124         FSCRYPT_LOGE("open policy file failed, errno: %d", errno);
125         return FSCRYPT_INVALID;
126     }
127 
128 #ifdef SUPPORT_FSCRYPT_V2
129     errno = 0;
130     (void)ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, NULL);
131     (void)close(fd);
132     if (errno == EOPNOTSUPP) {
133         FSCRYPT_LOGE("Kernel doesn't support fscrypt v1 or v2.");
134         return FSCRYPT_INVALID;
135     } else if (errno == ENOTTY) {
136         FSCRYPT_LOGE("Kernel doesn't support fscrypt v2, pls use v1.");
137         return FSCRYPT_V1;
138     } else if (errno == EFAULT) {
139         FSCRYPT_LOGI("Kernel is support fscrypt v2.");
140         return FSCRYPT_V2;
141     }
142     FSCRYPT_LOGE("Unexpected errno: %d", errno);
143     return FSCRYPT_INVALID;
144 #else
145     (void)close(fd);
146     return FSCRYPT_V1;
147 #endif
148 }
149 
KeyCtrlGetFscryptVersion(const char * mnt)150 uint8_t KeyCtrlGetFscryptVersion(const char *mnt)
151 {
152     uint8_t version = CheckKernelFscrypt(mnt);
153     return version;
154 }
155 
KeyCtrlHasFscryptSyspara(void)156 bool KeyCtrlHasFscryptSyspara(void)
157 {
158     FSCRYPT_LOGI("enter");
159     char tmp[POLICY_BUF_SIZE] = { 0 };
160     uint32_t len = POLICY_BUF_SIZE;
161     int ret = GetFscryptParameter(FSCRYPT_POLICY_KEY, "", tmp, &len);
162     if (ret != 0) {
163         FSCRYPT_LOGE("fscrypt config parameter not set, not enable fscrypt");
164         return false;
165     }
166 
167     return true;
168 }
169 
KeyCtrlLoadVersion(const char * keyPath)170 uint8_t KeyCtrlLoadVersion(const char *keyPath)
171 {
172     if (!keyPath) {
173         FSCRYPT_LOGE("key path is null");
174         return FSCRYPT_INVALID;
175     }
176     char pathLen = strlen(keyPath) + strlen(PATH_FSCRYPT_VER) + 1;
177     char *path = (char *)malloc(pathLen);
178     if (!path) {
179         FSCRYPT_LOGE("no memory for full key path");
180         return FSCRYPT_INVALID;
181     }
182     path[0] = '\0';
183     if (strncat_s(path, pathLen - strlen(PATH_FSCRYPT_VER), keyPath, strlen(keyPath)) != EOK) {
184         free(path);
185         FSCRYPT_LOGE("KEY path strcat error");
186         return FSCRYPT_INVALID;
187     }
188     if (strncat_s(path, pathLen, PATH_FSCRYPT_VER, strlen(PATH_FSCRYPT_VER)) != EOK) {
189         free(path);
190         FSCRYPT_LOGE("version path strcat error");
191         return FSCRYPT_INVALID;
192     }
193 
194     char *buf = ReadFileToBuf(path);
195     free(path);
196     if (!buf) {
197         FSCRYPT_LOGE("read fscrypt version file failed");
198         return FSCRYPT_INVALID;
199     }
200     if (isdigit(*buf)) {
201         int ver = atoi(buf);
202         if (ver == FSCRYPT_V1 || ver == FSCRYPT_V2) {
203             free(buf);
204             FSCRYPT_LOGI("version %d loaded", ver);
205             return ver;
206         }
207     }
208     free(buf);
209 
210     FSCRYPT_LOGE("bad version content");
211     return FSCRYPT_INVALID;
212 }
213