OSDN Git Service

Merge "Implement simpleperf record/dumprecord subcommands."
[android-x86/system-extras.git] / ext4_utils / ext4_crypt.cpp
1 /*
2  * Copyright (c) 2015 Google, Inc.
3  */
4
5 #define TAG "ext4_utils"
6
7 #include "ext4_crypt_init_extensions.h"
8
9 #include <dirent.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include <fcntl.h>
15 #include <asm/ioctl.h>
16 #include <sys/syscall.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19
20 #include <cutils/klog.h>
21
22 #include "unencrypted_properties.h"
23
24 #define XATTR_NAME_ENCRYPTION_POLICY "encryption.policy"
25 #define EXT4_KEYREF_DELIMITER ((char)'.')
26
27 // ext4enc:TODO Include structure from somewhere sensible
28 // MUST be in sync with ext4_crypto.c in kernel
29 #define EXT4_KEY_DESCRIPTOR_SIZE 8
30 struct ext4_encryption_policy {
31     char version;
32     char contents_encryption_mode;
33     char filenames_encryption_mode;
34     char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
35 } __attribute__((__packed__));
36
37 #define EXT4_ENCRYPTION_MODE_AES_256_XTS    1
38 #define EXT4_ENCRYPTION_MODE_AES_256_CTS    4
39
40 // ext4enc:TODO Get value from somewhere sensible
41 #define EXT4_IOC_SET_ENCRYPTION_POLICY \
42     _IOR('f', 19, struct ext4_encryption_policy)
43
44 /* Validate that all path items are available and accessible. */
45 static int is_path_valid(const char *path)
46 {
47     if (access(path, W_OK)) {
48         KLOG_ERROR(TAG, "Can't access %s: %s\n",strerror(errno), path);
49         return 0;
50     }
51
52     return 1;
53 }
54
55 static int is_dir_empty(const char *dirname)
56 {
57     int n = 0;
58     struct dirent *d;
59     DIR *dir;
60
61     dir = opendir(dirname);
62     while ((d = readdir(dir)) != NULL) {
63         if (strcmp(d->d_name, "lost+found") == 0) {
64             // Skip lost+found directory
65         } else if (++n > 2) {
66             break;
67         }
68     }
69     closedir(dir);
70     return n <= 2;
71 }
72
73 int do_policy_set(const char *directory, const char *policy, int policy_length)
74 {
75     struct stat st;
76     ssize_t ret;
77
78     if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
79         KLOG_ERROR("Policy wrong length\n");
80         return -EINVAL;
81     }
82
83     if (!is_path_valid(directory)) {
84         return -EINVAL;
85     }
86
87     stat(directory, &st);
88     if (!S_ISDIR(st.st_mode)) {
89         KLOG_ERROR(TAG, "Can only set policy on a directory (%s)\n", directory);
90         return -EINVAL;
91     }
92
93     if (!is_dir_empty(directory)) {
94         KLOG_ERROR(TAG, "Can only set policy on an empty directory (%s)\n",
95                    directory);
96         return -EINVAL;
97     }
98
99     int fd = open(directory, O_DIRECTORY);
100     if (fd == -1) {
101         KLOG_ERROR(TAG, "Failed to open directory (%s)\n", directory);
102         return -EINVAL;
103     }
104
105     ext4_encryption_policy eep;
106     eep.version = 0;
107     eep.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
108     eep.filenames_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_CTS;
109     memcpy(eep.master_key_descriptor, policy, EXT4_KEY_DESCRIPTOR_SIZE);
110     ret = ioctl(fd, EXT4_IOC_SET_ENCRYPTION_POLICY, &eep);
111     auto preserve_errno = errno;
112     close(fd);
113
114     if (ret) {
115         KLOG_ERROR(TAG, "Failed to set encryption policy for %s: %s\n",
116                    directory, strerror(preserve_errno));
117         return -EINVAL;
118     }
119
120     KLOG_INFO(TAG, "Encryption policy for %s is set to %s\n", directory, policy);
121     return 0;
122 }
123
124 bool e4crypt_non_default_key(const char* dir)
125 {
126     UnencryptedProperties props(dir);
127     return props.Get<int>(properties::is_default, 1) != 1;
128 }