blob: 886d17a1f14b8344c8fb6452a1d3d33195e7f7d2 [file] [log] [blame]
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +00001/*
2 * Copyright (c) 2015 Google, Inc.
3 */
4
Elliott Hughesee296862015-03-28 10:39:46 -07005#define TAG "ext4_utils"
6
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +00007#include "ext4_crypt_init_extensions.h"
Paul Lawrence92da49d2015-02-25 15:11:13 -08008
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +00009#include <dirent.h>
Paul Lawrence92da49d2015-02-25 15:11:13 -080010#include <errno.h>
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +000011#include <string.h>
12#include <unistd.h>
13
Paul Lawrencedb1d49c2015-04-10 07:47:30 -070014#include <fcntl.h>
15#include <asm/ioctl.h>
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +000016#include <sys/syscall.h>
17#include <sys/stat.h>
Paul Lawrencedb1d49c2015-04-10 07:47:30 -070018#include <sys/types.h>
Paul Lawrence92da49d2015-02-25 15:11:13 -080019
Elliott Hughesee296862015-03-28 10:39:46 -070020#include <cutils/klog.h>
Paul Lawrence92da49d2015-02-25 15:11:13 -080021
Paul Lawrence61980262015-03-16 15:35:55 -070022#include "unencrypted_properties.h"
Paul Lawrence92da49d2015-02-25 15:11:13 -080023
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +000024#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
Paul Lawrencedb1d49c2015-04-10 07:47:30 -070029#define EXT4_KEY_DESCRIPTOR_SIZE 8
30struct ext4_encryption_policy {
31 char version;
32 char contents_encryption_mode;
33 char filenames_encryption_mode;
Paul Lawrenceaf02e8a2015-05-01 05:24:04 -070034 char flags;
Paul Lawrencedb1d49c2015-04-10 07:47:30 -070035 char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
36} __attribute__((__packed__));
37
38#define EXT4_ENCRYPTION_MODE_AES_256_XTS 1
39#define EXT4_ENCRYPTION_MODE_AES_256_CTS 4
40
41// ext4enc:TODO Get value from somewhere sensible
42#define EXT4_IOC_SET_ENCRYPTION_POLICY \
43 _IOR('f', 19, struct ext4_encryption_policy)
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +000044
45/* Validate that all path items are available and accessible. */
46static int is_path_valid(const char *path)
47{
48 if (access(path, W_OK)) {
49 KLOG_ERROR(TAG, "Can't access %s: %s\n",strerror(errno), path);
50 return 0;
51 }
52
53 return 1;
54}
55
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +000056static int is_dir_empty(const char *dirname)
57{
58 int n = 0;
59 struct dirent *d;
60 DIR *dir;
61
62 dir = opendir(dirname);
63 while ((d = readdir(dir)) != NULL) {
64 if (strcmp(d->d_name, "lost+found") == 0) {
65 // Skip lost+found directory
66 } else if (++n > 2) {
67 break;
68 }
69 }
70 closedir(dir);
71 return n <= 2;
72}
73
Paul Lawrencedb1d49c2015-04-10 07:47:30 -070074int do_policy_set(const char *directory, const char *policy, int policy_length)
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +000075{
76 struct stat st;
77 ssize_t ret;
78
Paul Lawrencedb1d49c2015-04-10 07:47:30 -070079 if (policy_length != EXT4_KEY_DESCRIPTOR_SIZE) {
80 KLOG_ERROR("Policy wrong length\n");
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +000081 return -EINVAL;
82 }
83
84 if (!is_path_valid(directory)) {
85 return -EINVAL;
86 }
87
88 stat(directory, &st);
89 if (!S_ISDIR(st.st_mode)) {
90 KLOG_ERROR(TAG, "Can only set policy on a directory (%s)\n", directory);
91 return -EINVAL;
92 }
93
94 if (!is_dir_empty(directory)) {
95 KLOG_ERROR(TAG, "Can only set policy on an empty directory (%s)\n",
96 directory);
97 return -EINVAL;
98 }
99
Paul Lawrencedb1d49c2015-04-10 07:47:30 -0700100 int fd = open(directory, O_DIRECTORY);
101 if (fd == -1) {
102 KLOG_ERROR(TAG, "Failed to open directory (%s)\n", directory);
103 return -EINVAL;
104 }
105
106 ext4_encryption_policy eep;
107 eep.version = 0;
108 eep.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
109 eep.filenames_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_CTS;
Paul Lawrenceaf02e8a2015-05-01 05:24:04 -0700110 eep.flags = 0;
Paul Lawrencedb1d49c2015-04-10 07:47:30 -0700111 memcpy(eep.master_key_descriptor, policy, EXT4_KEY_DESCRIPTOR_SIZE);
112 ret = ioctl(fd, EXT4_IOC_SET_ENCRYPTION_POLICY, &eep);
113 auto preserve_errno = errno;
114 close(fd);
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +0000115
116 if (ret) {
117 KLOG_ERROR(TAG, "Failed to set encryption policy for %s: %s\n",
Paul Lawrencedb1d49c2015-04-10 07:47:30 -0700118 directory, strerror(preserve_errno));
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +0000119 return -EINVAL;
120 }
121
Paul Lawrenceaf02e8a2015-05-01 05:24:04 -0700122 KLOG_INFO(TAG, "Encryption policy for %s is set to %02x%02x%02x%02x\n",
123 directory, policy[0], policy[1], policy[2], policy[3]);
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +0000124 return 0;
Paul Lawrence61980262015-03-16 15:35:55 -0700125}
Paul Lawrence92da49d2015-02-25 15:11:13 -0800126
127bool e4crypt_non_default_key(const char* dir)
128{
Paul Lawrencebc2eb8c2015-04-28 22:13:04 +0000129 UnencryptedProperties props(dir);
130 return props.Get<int>(properties::is_default, 1) != 1;
Paul Lawrence92da49d2015-02-25 15:11:13 -0800131}