2 * Encryption policy functions for per-file encryption support.
4 * Copyright (C) 2015, Google, Inc.
5 * Copyright (C) 2015, Motorola Mobility.
7 * Written by Michael Halcrow, 2015.
8 * Modified by Jaegeuk Kim, 2015.
11 #include <linux/random.h>
12 #include <linux/string.h>
13 #include <linux/mount.h>
14 #include "fscrypt_private.h"
17 * check whether an encryption policy is consistent with an encryption context
19 static bool is_encryption_context_consistent_with_policy(
20 const struct fscrypt_context
*ctx
,
21 const struct fscrypt_policy
*policy
)
23 return memcmp(ctx
->master_key_descriptor
, policy
->master_key_descriptor
,
24 FS_KEY_DESCRIPTOR_SIZE
) == 0 &&
25 (ctx
->flags
== policy
->flags
) &&
26 (ctx
->contents_encryption_mode
==
27 policy
->contents_encryption_mode
) &&
28 (ctx
->filenames_encryption_mode
==
29 policy
->filenames_encryption_mode
);
32 static int create_encryption_context_from_policy(struct inode
*inode
,
33 const struct fscrypt_policy
*policy
)
35 struct fscrypt_context ctx
;
37 if (!inode
->i_sb
->s_cop
->set_context
)
40 ctx
.format
= FS_ENCRYPTION_CONTEXT_FORMAT_V1
;
41 memcpy(ctx
.master_key_descriptor
, policy
->master_key_descriptor
,
42 FS_KEY_DESCRIPTOR_SIZE
);
44 if (!fscrypt_valid_contents_enc_mode(
45 policy
->contents_encryption_mode
))
48 if (!fscrypt_valid_filenames_enc_mode(
49 policy
->filenames_encryption_mode
))
52 if (policy
->flags
& ~FS_POLICY_FLAGS_VALID
)
55 ctx
.contents_encryption_mode
= policy
->contents_encryption_mode
;
56 ctx
.filenames_encryption_mode
= policy
->filenames_encryption_mode
;
57 ctx
.flags
= policy
->flags
;
58 BUILD_BUG_ON(sizeof(ctx
.nonce
) != FS_KEY_DERIVATION_NONCE_SIZE
);
59 get_random_bytes(ctx
.nonce
, FS_KEY_DERIVATION_NONCE_SIZE
);
61 return inode
->i_sb
->s_cop
->set_context(inode
, &ctx
, sizeof(ctx
), NULL
);
64 int fscrypt_ioctl_set_policy(struct file
*filp
, const void __user
*arg
)
66 struct fscrypt_policy policy
;
67 struct inode
*inode
= file_inode(filp
);
69 struct fscrypt_context ctx
;
71 if (copy_from_user(&policy
, arg
, sizeof(policy
)))
74 if (!inode_owner_or_capable(inode
))
77 if (policy
.version
!= 0)
80 ret
= mnt_want_write_file(filp
);
86 ret
= inode
->i_sb
->s_cop
->get_context(inode
, &ctx
, sizeof(ctx
));
87 if (ret
== -ENODATA
) {
88 if (!S_ISDIR(inode
->i_mode
))
90 else if (!inode
->i_sb
->s_cop
->empty_dir
)
92 else if (!inode
->i_sb
->s_cop
->empty_dir(inode
))
95 ret
= create_encryption_context_from_policy(inode
,
97 } else if (ret
== sizeof(ctx
) &&
98 is_encryption_context_consistent_with_policy(&ctx
,
100 /* The file already uses the same encryption policy. */
102 } else if (ret
>= 0 || ret
== -ERANGE
) {
103 /* The file already uses a different encryption policy. */
109 mnt_drop_write_file(filp
);
112 EXPORT_SYMBOL(fscrypt_ioctl_set_policy
);
114 int fscrypt_ioctl_get_policy(struct file
*filp
, void __user
*arg
)
116 struct inode
*inode
= file_inode(filp
);
117 struct fscrypt_context ctx
;
118 struct fscrypt_policy policy
;
121 if (!inode
->i_sb
->s_cop
->get_context
||
122 !inode
->i_sb
->s_cop
->is_encrypted(inode
))
125 res
= inode
->i_sb
->s_cop
->get_context(inode
, &ctx
, sizeof(ctx
));
126 if (res
< 0 && res
!= -ERANGE
)
128 if (res
!= sizeof(ctx
))
130 if (ctx
.format
!= FS_ENCRYPTION_CONTEXT_FORMAT_V1
)
134 policy
.contents_encryption_mode
= ctx
.contents_encryption_mode
;
135 policy
.filenames_encryption_mode
= ctx
.filenames_encryption_mode
;
136 policy
.flags
= ctx
.flags
;
137 memcpy(policy
.master_key_descriptor
, ctx
.master_key_descriptor
,
138 FS_KEY_DESCRIPTOR_SIZE
);
140 if (copy_to_user(arg
, &policy
, sizeof(policy
)))
144 EXPORT_SYMBOL(fscrypt_ioctl_get_policy
);
146 int fscrypt_has_permitted_context(struct inode
*parent
, struct inode
*child
)
148 struct fscrypt_info
*parent_ci
, *child_ci
;
151 if ((parent
== NULL
) || (child
== NULL
)) {
152 printk(KERN_ERR
"parent %p child %p\n", parent
, child
);
156 /* No restrictions on file types which are never encrypted */
157 if (!S_ISREG(child
->i_mode
) && !S_ISDIR(child
->i_mode
) &&
158 !S_ISLNK(child
->i_mode
))
161 /* no restrictions if the parent directory is not encrypted */
162 if (!parent
->i_sb
->s_cop
->is_encrypted(parent
))
164 /* if the child directory is not encrypted, this is always a problem */
165 if (!parent
->i_sb
->s_cop
->is_encrypted(child
))
167 res
= fscrypt_get_encryption_info(parent
);
170 res
= fscrypt_get_encryption_info(child
);
173 parent_ci
= parent
->i_crypt_info
;
174 child_ci
= child
->i_crypt_info
;
175 if (!parent_ci
&& !child_ci
)
177 if (!parent_ci
|| !child_ci
)
180 return (memcmp(parent_ci
->ci_master_key
,
181 child_ci
->ci_master_key
,
182 FS_KEY_DESCRIPTOR_SIZE
) == 0 &&
183 (parent_ci
->ci_data_mode
== child_ci
->ci_data_mode
) &&
184 (parent_ci
->ci_filename_mode
== child_ci
->ci_filename_mode
) &&
185 (parent_ci
->ci_flags
== child_ci
->ci_flags
));
187 EXPORT_SYMBOL(fscrypt_has_permitted_context
);
190 * fscrypt_inherit_context() - Sets a child context from its parent
191 * @parent: Parent inode from which the context is inherited.
192 * @child: Child inode that inherits the context from @parent.
193 * @fs_data: private data given by FS.
194 * @preload: preload child i_crypt_info if true
196 * Return: 0 on success, -errno on failure
198 int fscrypt_inherit_context(struct inode
*parent
, struct inode
*child
,
199 void *fs_data
, bool preload
)
201 struct fscrypt_context ctx
;
202 struct fscrypt_info
*ci
;
205 if (!parent
->i_sb
->s_cop
->set_context
)
208 res
= fscrypt_get_encryption_info(parent
);
212 ci
= parent
->i_crypt_info
;
216 ctx
.format
= FS_ENCRYPTION_CONTEXT_FORMAT_V1
;
217 ctx
.contents_encryption_mode
= ci
->ci_data_mode
;
218 ctx
.filenames_encryption_mode
= ci
->ci_filename_mode
;
219 ctx
.flags
= ci
->ci_flags
;
220 memcpy(ctx
.master_key_descriptor
, ci
->ci_master_key
,
221 FS_KEY_DESCRIPTOR_SIZE
);
222 get_random_bytes(ctx
.nonce
, FS_KEY_DERIVATION_NONCE_SIZE
);
223 res
= parent
->i_sb
->s_cop
->set_context(child
, &ctx
,
224 sizeof(ctx
), fs_data
);
227 return preload
? fscrypt_get_encryption_info(child
): 0;
229 EXPORT_SYMBOL(fscrypt_inherit_context
);