Commit | Line | Data |
---|---|---|
9bd8212f MH |
1 | /* |
2 | * linux/fs/ext4/crypto_policy.c | |
3 | * | |
4 | * Copyright (C) 2015, Google, Inc. | |
5 | * | |
6 | * This contains encryption policy functions for ext4 | |
7 | * | |
8 | * Written by Michael Halcrow, 2015. | |
9 | */ | |
10 | ||
11 | #include <linux/random.h> | |
12 | #include <linux/string.h> | |
13 | #include <linux/types.h> | |
14 | ||
15 | #include "ext4.h" | |
16 | #include "xattr.h" | |
17 | ||
18 | static int ext4_inode_has_encryption_context(struct inode *inode) | |
19 | { | |
20 | int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, | |
21 | EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, NULL, 0); | |
22 | return (res > 0); | |
23 | } | |
24 | ||
25 | /* | |
26 | * check whether the policy is consistent with the encryption context | |
27 | * for the inode | |
28 | */ | |
29 | static int ext4_is_encryption_context_consistent_with_policy( | |
30 | struct inode *inode, const struct ext4_encryption_policy *policy) | |
31 | { | |
32 | struct ext4_encryption_context ctx; | |
33 | int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, | |
34 | EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx, | |
35 | sizeof(ctx)); | |
36 | if (res != sizeof(ctx)) | |
37 | return 0; | |
38 | return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor, | |
39 | EXT4_KEY_DESCRIPTOR_SIZE) == 0 && | |
40 | (ctx.contents_encryption_mode == | |
41 | policy->contents_encryption_mode) && | |
42 | (ctx.filenames_encryption_mode == | |
43 | policy->filenames_encryption_mode)); | |
44 | } | |
45 | ||
46 | static int ext4_create_encryption_context_from_policy( | |
47 | struct inode *inode, const struct ext4_encryption_policy *policy) | |
48 | { | |
49 | struct ext4_encryption_context ctx; | |
50 | int res = 0; | |
51 | ||
52 | ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1; | |
53 | memcpy(ctx.master_key_descriptor, policy->master_key_descriptor, | |
54 | EXT4_KEY_DESCRIPTOR_SIZE); | |
b30ab0e0 MH |
55 | if (!ext4_valid_contents_enc_mode(policy->contents_encryption_mode)) { |
56 | printk(KERN_WARNING | |
57 | "%s: Invalid contents encryption mode %d\n", __func__, | |
58 | policy->contents_encryption_mode); | |
59 | res = -EINVAL; | |
60 | goto out; | |
61 | } | |
9bd8212f MH |
62 | ctx.contents_encryption_mode = policy->contents_encryption_mode; |
63 | ctx.filenames_encryption_mode = policy->filenames_encryption_mode; | |
64 | BUILD_BUG_ON(sizeof(ctx.nonce) != EXT4_KEY_DERIVATION_NONCE_SIZE); | |
65 | get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE); | |
66 | ||
67 | res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION, | |
68 | EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx, | |
69 | sizeof(ctx), 0); | |
b30ab0e0 | 70 | out: |
9bd8212f MH |
71 | if (!res) |
72 | ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT); | |
73 | return res; | |
74 | } | |
75 | ||
76 | int ext4_process_policy(const struct ext4_encryption_policy *policy, | |
77 | struct inode *inode) | |
78 | { | |
79 | if (policy->version != 0) | |
80 | return -EINVAL; | |
81 | ||
82 | if (!ext4_inode_has_encryption_context(inode)) { | |
83 | if (!ext4_empty_dir(inode)) | |
84 | return -ENOTEMPTY; | |
85 | return ext4_create_encryption_context_from_policy(inode, | |
86 | policy); | |
87 | } | |
88 | ||
89 | if (ext4_is_encryption_context_consistent_with_policy(inode, policy)) | |
90 | return 0; | |
91 | ||
92 | printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n", | |
93 | __func__); | |
94 | return -EINVAL; | |
95 | } | |
96 | ||
97 | int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy) | |
98 | { | |
99 | struct ext4_encryption_context ctx; | |
100 | ||
101 | int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, | |
102 | EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, | |
103 | &ctx, sizeof(ctx)); | |
104 | if (res != sizeof(ctx)) | |
105 | return -ENOENT; | |
106 | if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1) | |
107 | return -EINVAL; | |
108 | policy->version = 0; | |
109 | policy->contents_encryption_mode = ctx.contents_encryption_mode; | |
110 | policy->filenames_encryption_mode = ctx.filenames_encryption_mode; | |
111 | memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor, | |
112 | EXT4_KEY_DESCRIPTOR_SIZE); | |
113 | return 0; | |
114 | } | |
115 | ||
116 | int ext4_is_child_context_consistent_with_parent(struct inode *parent, | |
117 | struct inode *child) | |
118 | { | |
119 | struct ext4_encryption_context parent_ctx, child_ctx; | |
120 | int res; | |
121 | ||
122 | if ((parent == NULL) || (child == NULL)) { | |
123 | pr_err("parent %p child %p\n", parent, child); | |
124 | BUG_ON(1); | |
125 | } | |
126 | /* no restrictions if the parent directory is not encrypted */ | |
127 | if (!ext4_encrypted_inode(parent)) | |
128 | return 1; | |
129 | res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION, | |
130 | EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, | |
131 | &parent_ctx, sizeof(parent_ctx)); | |
132 | if (res != sizeof(parent_ctx)) | |
133 | return 0; | |
134 | /* if the child directory is not encrypted, this is always a problem */ | |
135 | if (!ext4_encrypted_inode(child)) | |
136 | return 0; | |
137 | res = ext4_xattr_get(child, EXT4_XATTR_INDEX_ENCRYPTION, | |
138 | EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, | |
139 | &child_ctx, sizeof(child_ctx)); | |
140 | if (res != sizeof(child_ctx)) | |
141 | return 0; | |
142 | return (memcmp(parent_ctx.master_key_descriptor, | |
143 | child_ctx.master_key_descriptor, | |
144 | EXT4_KEY_DESCRIPTOR_SIZE) == 0 && | |
145 | (parent_ctx.contents_encryption_mode == | |
146 | child_ctx.contents_encryption_mode) && | |
147 | (parent_ctx.filenames_encryption_mode == | |
148 | child_ctx.filenames_encryption_mode)); | |
149 | } | |
150 | ||
151 | /** | |
152 | * ext4_inherit_context() - Sets a child context from its parent | |
153 | * @parent: Parent inode from which the context is inherited. | |
154 | * @child: Child inode that inherits the context from @parent. | |
155 | * | |
156 | * Return: Zero on success, non-zero otherwise | |
157 | */ | |
158 | int ext4_inherit_context(struct inode *parent, struct inode *child) | |
159 | { | |
160 | struct ext4_encryption_context ctx; | |
161 | int res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION, | |
162 | EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, | |
163 | &ctx, sizeof(ctx)); | |
164 | ||
165 | if (res != sizeof(ctx)) | |
166 | return -ENOENT; | |
167 | ||
168 | get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE); | |
169 | res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION, | |
170 | EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx, | |
171 | sizeof(ctx), 0); | |
172 | if (!res) | |
173 | ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT); | |
174 | return res; | |
175 | } |