• 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     char *realPath = realpath(mnt, NULL);
68     if (realPath == NULL) {
69         FSCRYPT_LOGE("realpath failed");
70         return false;
71     }
72 
73     int fd = open(realPath, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
74     free(realPath);
75     if (fd < 0) {
76         FSCRYPT_LOGE("open %s failed, errno:%d", mnt, errno);
77         return false;
78     }
79     if (ioctl(fd, cmd, arg) != 0) {
80         FSCRYPT_LOGE("ioctl to %s failed, errno:%d", mnt, errno);
81         (void)close(fd);
82         return false;
83     }
84     (void)close(fd);
85     FSCRYPT_LOGI("success");
86     return true;
87 }
88 
89 #ifdef SUPPORT_FSCRYPT_V2
KeyCtrlInstallKey(const char * mnt,struct fscrypt_add_key_arg * arg)90 bool KeyCtrlInstallKey(const char *mnt, struct fscrypt_add_key_arg *arg)
91 {
92     FSCRYPT_LOGI("enter");
93     return FsIoctl(mnt, FS_IOC_ADD_ENCRYPTION_KEY, (void *)(arg));
94 }
95 
KeyCtrlRemoveKey(const char * mnt,struct fscrypt_remove_key_arg * arg)96 bool KeyCtrlRemoveKey(const char *mnt, struct fscrypt_remove_key_arg *arg)
97 {
98     FSCRYPT_LOGI("enter");
99     return FsIoctl(mnt, FS_IOC_REMOVE_ENCRYPTION_KEY, (void *)arg);
100 }
101 
KeyCtrlGetKeyStatus(const char * mnt,struct fscrypt_get_key_status_arg * arg)102 bool KeyCtrlGetKeyStatus(const char *mnt, struct fscrypt_get_key_status_arg *arg)
103 {
104     FSCRYPT_LOGI("enter");
105     return FsIoctl(mnt, FS_IOC_GET_ENCRYPTION_KEY_STATUS, (void *)(arg));
106 }
107 
KeyCtrlGetPolicyEx(const char * path,struct fscrypt_get_policy_ex_arg * policy)108 bool KeyCtrlGetPolicyEx(const char *path, struct fscrypt_get_policy_ex_arg *policy)
109 {
110     FSCRYPT_LOGI("enter");
111     return FsIoctl(path, FS_IOC_GET_ENCRYPTION_POLICY_EX, (void *)(policy));
112 }
113 #endif
114 
KeyCtrlSetPolicy(const char * path,union FscryptPolicy * policy)115 bool KeyCtrlSetPolicy(const char *path, union FscryptPolicy *policy)
116 {
117     FSCRYPT_LOGI("enter");
118     return FsIoctl(path, FS_IOC_SET_ENCRYPTION_POLICY, (void *)(policy));
119 }
120 
KeyCtrlGetPolicy(const char * path,struct fscrypt_policy * policy)121 bool KeyCtrlGetPolicy(const char *path, struct fscrypt_policy *policy)
122 {
123     FSCRYPT_LOGI("enter");
124     return FsIoctl(path, FS_IOC_GET_ENCRYPTION_POLICY, (void *)(policy));
125 }
126 
CheckKernelFscrypt(const char * mnt)127 static uint8_t CheckKernelFscrypt(const char *mnt)
128 {
129     char *realPath = realpath(mnt, NULL);
130     if (realPath == NULL) {
131         FSCRYPT_LOGE("realpath failed");
132         return FSCRYPT_INVALID;
133     }
134 
135     int fd = open(realPath, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
136     free(realPath);
137     if (fd < 0) {
138         FSCRYPT_LOGE("open policy file failed, errno: %d", errno);
139         return FSCRYPT_INVALID;
140     }
141 
142 #ifdef SUPPORT_FSCRYPT_V2
143     errno = 0;
144     (void)ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, NULL);
145     (void)close(fd);
146     if (errno == EOPNOTSUPP) {
147         FSCRYPT_LOGE("Kernel doesn't support fscrypt v1 or v2.");
148         return FSCRYPT_INVALID;
149     } else if (errno == ENOTTY) {
150         FSCRYPT_LOGE("Kernel doesn't support fscrypt v2, pls use v1.");
151         return FSCRYPT_V1;
152     } else if (errno == EFAULT) {
153         FSCRYPT_LOGI("Kernel is support fscrypt v2.");
154         return FSCRYPT_V2;
155     }
156     FSCRYPT_LOGE("Unexpected errno: %d", errno);
157     return FSCRYPT_INVALID;
158 #else
159     (void)close(fd);
160     return FSCRYPT_V1;
161 #endif
162 }
163 
KeyCtrlGetFscryptVersion(const char * mnt)164 uint8_t KeyCtrlGetFscryptVersion(const char *mnt)
165 {
166     uint8_t version = CheckKernelFscrypt(mnt);
167     return version;
168 }
169 
KeyCtrlHasFscryptSyspara(void)170 bool KeyCtrlHasFscryptSyspara(void)
171 {
172     FSCRYPT_LOGI("enter");
173     char tmp[POLICY_BUF_SIZE] = { 0 };
174     uint32_t len = POLICY_BUF_SIZE;
175     int ret = GetFscryptParameter(FSCRYPT_POLICY_KEY, "", tmp, &len);
176     if (ret != 0) {
177         FSCRYPT_LOGE("fscrypt config parameter not set, not enable fscrypt");
178         return false;
179     }
180 
181     return true;
182 }
183 
KeyCtrlLoadVersion(const char * keyPath)184 uint8_t KeyCtrlLoadVersion(const char *keyPath)
185 {
186     if (!keyPath) {
187         FSCRYPT_LOGE("key path is null");
188         return FSCRYPT_INVALID;
189     }
190     char pathLen = strlen(keyPath) + strlen(PATH_FSCRYPT_VER) + 1;
191     char *path = (char *)malloc(pathLen);
192     if (!path) {
193         FSCRYPT_LOGE("no memory for full key path");
194         return FSCRYPT_INVALID;
195     }
196     path[0] = '\0';
197     if (strncat_s(path, pathLen - strlen(PATH_FSCRYPT_VER), keyPath, strlen(keyPath)) != EOK) {
198         free(path);
199         FSCRYPT_LOGE("KEY path strcat error");
200         return FSCRYPT_INVALID;
201     }
202     if (strncat_s(path, pathLen, PATH_FSCRYPT_VER, strlen(PATH_FSCRYPT_VER)) != EOK) {
203         free(path);
204         FSCRYPT_LOGE("version path strcat error");
205         return FSCRYPT_INVALID;
206     }
207 
208     char *buf = ReadFileToBuf(path);
209     free(path);
210     if (!buf) {
211         FSCRYPT_LOGE("read fscrypt version file failed");
212         return FSCRYPT_INVALID;
213     }
214     if (isdigit(*buf)) {
215         int ver = atoi(buf);
216         if (ver == FSCRYPT_V1 || ver == FSCRYPT_V2) {
217             free(buf);
218             FSCRYPT_LOGI("version %d loaded", ver);
219             return ver;
220         }
221     }
222     free(buf);
223 
224     FSCRYPT_LOGE("bad version content");
225     return FSCRYPT_INVALID;
226 }
227