Skip to content

Commit b59db43

Browse files
committed
eCryptfs: Prevent file create race condition
The file creation path prematurely called d_instantiate() and unlock_new_inode() before the eCryptfs inode info was fully allocated and initialized and before the eCryptfs metadata was written to the lower file. This could result in race conditions in subsequent file and inode operations leading to unexpected error conditions or a null pointer dereference while attempting to use the unallocated memory. https://launchpad.net/bugs/813146 Signed-off-by: Tyler Hicks <tyhicks@canonical.com> Cc: stable@kernel.org
1 parent 6fe4c6d commit b59db43

3 files changed

Lines changed: 46 additions & 33 deletions

File tree

‎fs/ecryptfs/crypto.c‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ static void ecryptfs_set_default_crypt_stat_vals(
967967

968968
/**
969969
* ecryptfs_new_file_context
970-
* @ecryptfs_dentry: The eCryptfs dentry
970+
* @ecryptfs_inode: The eCryptfs inode
971971
*
972972
* If the crypto context for the file has not yet been established,
973973
* this is where we do that. Establishing a new crypto context
@@ -984,13 +984,13 @@ static void ecryptfs_set_default_crypt_stat_vals(
984984
*
985985
* Returns zero on success; non-zero otherwise
986986
*/
987-
int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry)
987+
int ecryptfs_new_file_context(struct inode *ecryptfs_inode)
988988
{
989989
struct ecryptfs_crypt_stat *crypt_stat =
990-
&ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
990+
&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
991991
struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
992992
&ecryptfs_superblock_to_private(
993-
ecryptfs_dentry->d_sb)->mount_crypt_stat;
993+
ecryptfs_inode->i_sb)->mount_crypt_stat;
994994
int cipher_name_len;
995995
int rc = 0;
996996

@@ -1299,12 +1299,12 @@ static int ecryptfs_write_headers_virt(char *page_virt, size_t max,
12991299
}
13001300

13011301
static int
1302-
ecryptfs_write_metadata_to_contents(struct dentry *ecryptfs_dentry,
1302+
ecryptfs_write_metadata_to_contents(struct inode *ecryptfs_inode,
13031303
char *virt, size_t virt_len)
13041304
{
13051305
int rc;
13061306

1307-
rc = ecryptfs_write_lower(ecryptfs_dentry->d_inode, virt,
1307+
rc = ecryptfs_write_lower(ecryptfs_inode, virt,
13081308
0, virt_len);
13091309
if (rc < 0)
13101310
printk(KERN_ERR "%s: Error attempting to write header "
@@ -1338,7 +1338,8 @@ static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
13381338

13391339
/**
13401340
* ecryptfs_write_metadata
1341-
* @ecryptfs_dentry: The eCryptfs dentry
1341+
* @ecryptfs_dentry: The eCryptfs dentry, which should be negative
1342+
* @ecryptfs_inode: The newly created eCryptfs inode
13421343
*
13431344
* Write the file headers out. This will likely involve a userspace
13441345
* callout, in which the session key is encrypted with one or more
@@ -1348,10 +1349,11 @@ static unsigned long ecryptfs_get_zeroed_pages(gfp_t gfp_mask,
13481349
*
13491350
* Returns zero on success; non-zero on error
13501351
*/
1351-
int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
1352+
int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
1353+
struct inode *ecryptfs_inode)
13521354
{
13531355
struct ecryptfs_crypt_stat *crypt_stat =
1354-
&ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
1356+
&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
13551357
unsigned int order;
13561358
char *virt;
13571359
size_t virt_len;
@@ -1391,7 +1393,7 @@ int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
13911393
rc = ecryptfs_write_metadata_to_xattr(ecryptfs_dentry, virt,
13921394
size);
13931395
else
1394-
rc = ecryptfs_write_metadata_to_contents(ecryptfs_dentry, virt,
1396+
rc = ecryptfs_write_metadata_to_contents(ecryptfs_inode, virt,
13951397
virt_len);
13961398
if (rc) {
13971399
printk(KERN_ERR "%s: Error writing metadata out to lower file; "

‎fs/ecryptfs/ecryptfs_kernel.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,10 @@ int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat);
584584
int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode);
585585
int ecryptfs_encrypt_page(struct page *page);
586586
int ecryptfs_decrypt_page(struct page *page);
587-
int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry);
587+
int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry,
588+
struct inode *ecryptfs_inode);
588589
int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry);
589-
int ecryptfs_new_file_context(struct dentry *ecryptfs_dentry);
590+
int ecryptfs_new_file_context(struct inode *ecryptfs_inode);
590591
void ecryptfs_write_crypt_stat_flags(char *page_virt,
591592
struct ecryptfs_crypt_stat *crypt_stat,
592593
size_t *written);

‎fs/ecryptfs/inode.c‎

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -172,43 +172,43 @@ ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
172172
* it. It will also update the eCryptfs directory inode to mimic the
173173
* stat of the lower directory inode.
174174
*
175-
* Returns zero on success; non-zero on error condition
175+
* Returns the new eCryptfs inode on success; an ERR_PTR on error condition
176176
*/
177-
static int
177+
static struct inode *
178178
ecryptfs_do_create(struct inode *directory_inode,
179179
struct dentry *ecryptfs_dentry, int mode)
180180
{
181181
int rc;
182182
struct dentry *lower_dentry;
183183
struct dentry *lower_dir_dentry;
184+
struct inode *inode;
184185

185186
lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
186187
lower_dir_dentry = lock_parent(lower_dentry);
187188
if (IS_ERR(lower_dir_dentry)) {
188189
ecryptfs_printk(KERN_ERR, "Error locking directory of "
189190
"dentry\n");
190-
rc = PTR_ERR(lower_dir_dentry);
191+
inode = ERR_CAST(lower_dir_dentry);
191192
goto out;
192193
}
193194
rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode,
194195
ecryptfs_dentry, mode);
195196
if (rc) {
196197
printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
197198
"rc = [%d]\n", __func__, rc);
199+
inode = ERR_PTR(rc);
198200
goto out_lock;
199201
}
200-
rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
201-
directory_inode->i_sb);
202-
if (rc) {
203-
ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
202+
inode = __ecryptfs_get_inode(lower_dentry->d_inode,
203+
directory_inode->i_sb);
204+
if (IS_ERR(inode))
204205
goto out_lock;
205-
}
206206
fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
207207
fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
208208
out_lock:
209209
unlock_dir(lower_dir_dentry);
210210
out:
211-
return rc;
211+
return inode;
212212
}
213213

214214
/**
@@ -219,37 +219,37 @@ ecryptfs_do_create(struct inode *directory_inode,
219219
*
220220
* Returns zero on success
221221
*/
222-
static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
222+
static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
223+
struct inode *ecryptfs_inode)
223224
{
224225
struct ecryptfs_crypt_stat *crypt_stat =
225-
&ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
226+
&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
226227
int rc = 0;
227228

228-
if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
229+
if (S_ISDIR(ecryptfs_inode->i_mode)) {
229230
ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
230231
crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
231232
goto out;
232233
}
233234
ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
234-
rc = ecryptfs_new_file_context(ecryptfs_dentry);
235+
rc = ecryptfs_new_file_context(ecryptfs_inode);
235236
if (rc) {
236237
ecryptfs_printk(KERN_ERR, "Error creating new file "
237238
"context; rc = [%d]\n", rc);
238239
goto out;
239240
}
240-
rc = ecryptfs_get_lower_file(ecryptfs_dentry,
241-
ecryptfs_dentry->d_inode);
241+
rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
242242
if (rc) {
243243
printk(KERN_ERR "%s: Error attempting to initialize "
244244
"the lower file for the dentry with name "
245245
"[%s]; rc = [%d]\n", __func__,
246246
ecryptfs_dentry->d_name.name, rc);
247247
goto out;
248248
}
249-
rc = ecryptfs_write_metadata(ecryptfs_dentry);
249+
rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
250250
if (rc)
251251
printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
252-
ecryptfs_put_lower_file(ecryptfs_dentry->d_inode);
252+
ecryptfs_put_lower_file(ecryptfs_inode);
253253
out:
254254
return rc;
255255
}
@@ -269,18 +269,28 @@ static int
269269
ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
270270
int mode, struct nameidata *nd)
271271
{
272+
struct inode *ecryptfs_inode;
272273
int rc;
273274

274-
/* ecryptfs_do_create() calls ecryptfs_interpose() */
275-
rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode);
276-
if (unlikely(rc)) {
275+
ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
276+
mode);
277+
if (unlikely(IS_ERR(ecryptfs_inode))) {
277278
ecryptfs_printk(KERN_WARNING, "Failed to create file in"
278279
"lower filesystem\n");
280+
rc = PTR_ERR(ecryptfs_inode);
279281
goto out;
280282
}
281283
/* At this point, a file exists on "disk"; we need to make sure
282284
* that this on disk file is prepared to be an ecryptfs file */
283-
rc = ecryptfs_initialize_file(ecryptfs_dentry);
285+
rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
286+
if (rc) {
287+
drop_nlink(ecryptfs_inode);
288+
unlock_new_inode(ecryptfs_inode);
289+
iput(ecryptfs_inode);
290+
goto out;
291+
}
292+
d_instantiate(ecryptfs_dentry, ecryptfs_inode);
293+
unlock_new_inode(ecryptfs_inode);
284294
out:
285295
return rc;
286296
}

0 commit comments

Comments
 (0)