14
14
#include <linux/posix_acl_xattr.h>
15
15
#include <linux/atomic.h>
16
16
#include <linux/ratelimit.h>
17
+ #include <linux/backing-file.h>
17
18
#include "overlayfs.h"
18
19
19
20
static unsigned short ovl_redirect_max = 256 ;
@@ -260,14 +261,13 @@ static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
260
261
* may not use to instantiate the new dentry.
261
262
*/
262
263
static int ovl_instantiate (struct dentry * dentry , struct inode * inode ,
263
- struct dentry * newdentry , bool hardlink )
264
+ struct dentry * newdentry , bool hardlink , struct file * tmpfile )
264
265
{
265
266
struct ovl_inode_params oip = {
266
267
.upperdentry = newdentry ,
267
268
.newinode = inode ,
268
269
};
269
270
270
- ovl_dir_modified (dentry -> d_parent , false);
271
271
ovl_dentry_set_upper_alias (dentry );
272
272
ovl_dentry_init_reval (dentry , newdentry , NULL );
273
273
@@ -295,6 +295,9 @@ static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
295
295
inc_nlink (inode );
296
296
}
297
297
298
+ if (tmpfile )
299
+ d_mark_tmpfile (tmpfile , inode );
300
+
298
301
d_instantiate (dentry , inode );
299
302
if (inode != oip .newinode ) {
300
303
pr_warn_ratelimited ("newly created inode found in cache (%pd2)\n" ,
@@ -327,9 +330,6 @@ static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
327
330
struct dentry * newdentry ;
328
331
int err ;
329
332
330
- if (!attr -> hardlink && !IS_POSIXACL (udir ))
331
- attr -> mode &= ~current_umask ();
332
-
333
333
inode_lock_nested (udir , I_MUTEX_PARENT );
334
334
newdentry = ovl_create_real (ofs , udir ,
335
335
ovl_lookup_upper (ofs , dentry -> d_name .name ,
@@ -345,7 +345,8 @@ static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
345
345
ovl_set_opaque (dentry , newdentry );
346
346
}
347
347
348
- err = ovl_instantiate (dentry , inode , newdentry , !!attr -> hardlink );
348
+ ovl_dir_modified (dentry -> d_parent , false);
349
+ err = ovl_instantiate (dentry , inode , newdentry , !!attr -> hardlink , NULL );
349
350
if (err )
350
351
goto out_cleanup ;
351
352
out_unlock :
@@ -529,7 +530,8 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
529
530
if (err )
530
531
goto out_cleanup ;
531
532
}
532
- err = ovl_instantiate (dentry , inode , newdentry , hardlink );
533
+ ovl_dir_modified (dentry -> d_parent , false);
534
+ err = ovl_instantiate (dentry , inode , newdentry , hardlink , NULL );
533
535
if (err ) {
534
536
ovl_cleanup (ofs , udir , newdentry );
535
537
dput (newdentry );
@@ -551,12 +553,35 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
551
553
goto out_dput ;
552
554
}
553
555
556
+ static int ovl_setup_cred_for_create (struct dentry * dentry , struct inode * inode ,
557
+ umode_t mode , const struct cred * old_cred )
558
+ {
559
+ int err ;
560
+ struct cred * override_cred ;
561
+
562
+ override_cred = prepare_creds ();
563
+ if (!override_cred )
564
+ return - ENOMEM ;
565
+
566
+ override_cred -> fsuid = inode -> i_uid ;
567
+ override_cred -> fsgid = inode -> i_gid ;
568
+ err = security_dentry_create_files_as (dentry , mode , & dentry -> d_name ,
569
+ old_cred , override_cred );
570
+ if (err ) {
571
+ put_cred (override_cred );
572
+ return err ;
573
+ }
574
+ put_cred (override_creds (override_cred ));
575
+ put_cred (override_cred );
576
+
577
+ return 0 ;
578
+ }
579
+
554
580
static int ovl_create_or_link (struct dentry * dentry , struct inode * inode ,
555
581
struct ovl_cattr * attr , bool origin )
556
582
{
557
583
int err ;
558
584
const struct cred * old_cred ;
559
- struct cred * override_cred ;
560
585
struct dentry * parent = dentry -> d_parent ;
561
586
562
587
old_cred = ovl_override_creds (dentry -> d_sb );
@@ -572,10 +597,6 @@ static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
572
597
}
573
598
574
599
if (!attr -> hardlink ) {
575
- err = - ENOMEM ;
576
- override_cred = prepare_creds ();
577
- if (!override_cred )
578
- goto out_revert_creds ;
579
600
/*
580
601
* In the creation cases(create, mkdir, mknod, symlink),
581
602
* ovl should transfer current's fs{u,g}id to underlying
@@ -589,17 +610,9 @@ static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
589
610
* create a new inode, so just use the ovl mounter's
590
611
* fs{u,g}id.
591
612
*/
592
- override_cred -> fsuid = inode -> i_uid ;
593
- override_cred -> fsgid = inode -> i_gid ;
594
- err = security_dentry_create_files_as (dentry ,
595
- attr -> mode , & dentry -> d_name , old_cred ,
596
- override_cred );
597
- if (err ) {
598
- put_cred (override_cred );
613
+ err = ovl_setup_cred_for_create (dentry , inode , attr -> mode , old_cred );
614
+ if (err )
599
615
goto out_revert_creds ;
600
- }
601
- put_cred (override_creds (override_cred ));
602
- put_cred (override_cred );
603
616
}
604
617
605
618
if (!ovl_dentry_is_whiteout (dentry ))
@@ -1290,6 +1303,100 @@ static int ovl_rename(struct mnt_idmap *idmap, struct inode *olddir,
1290
1303
return err ;
1291
1304
}
1292
1305
1306
+ static int ovl_create_tmpfile (struct file * file , struct dentry * dentry ,
1307
+ struct inode * inode , umode_t mode )
1308
+ {
1309
+ const struct cred * old_cred ;
1310
+ struct path realparentpath ;
1311
+ struct file * realfile ;
1312
+ struct dentry * newdentry ;
1313
+ /* It's okay to set O_NOATIME, since the owner will be current fsuid */
1314
+ int flags = file -> f_flags | OVL_OPEN_FLAGS ;
1315
+ int err ;
1316
+
1317
+ err = ovl_copy_up (dentry -> d_parent );
1318
+ if (err )
1319
+ return err ;
1320
+
1321
+ old_cred = ovl_override_creds (dentry -> d_sb );
1322
+ err = ovl_setup_cred_for_create (dentry , inode , mode , old_cred );
1323
+ if (err )
1324
+ goto out_revert_creds ;
1325
+
1326
+ ovl_path_upper (dentry -> d_parent , & realparentpath );
1327
+ realfile = backing_tmpfile_open (& file -> f_path , flags , & realparentpath ,
1328
+ mode , current_cred ());
1329
+ err = PTR_ERR_OR_ZERO (realfile );
1330
+ pr_debug ("tmpfile/open(%pd2, 0%o) = %i\n" , realparentpath .dentry , mode , err );
1331
+ if (err )
1332
+ goto out_revert_creds ;
1333
+
1334
+ /* ovl_instantiate() consumes the newdentry reference on success */
1335
+ newdentry = dget (realfile -> f_path .dentry );
1336
+ err = ovl_instantiate (dentry , inode , newdentry , false, file );
1337
+ if (!err ) {
1338
+ file -> private_data = realfile ;
1339
+ } else {
1340
+ dput (newdentry );
1341
+ fput (realfile );
1342
+ }
1343
+ out_revert_creds :
1344
+ revert_creds (old_cred );
1345
+ return err ;
1346
+ }
1347
+
1348
+ static int ovl_dummy_open (struct inode * inode , struct file * file )
1349
+ {
1350
+ return 0 ;
1351
+ }
1352
+
1353
+ static int ovl_tmpfile (struct mnt_idmap * idmap , struct inode * dir ,
1354
+ struct file * file , umode_t mode )
1355
+ {
1356
+ int err ;
1357
+ struct dentry * dentry = file -> f_path .dentry ;
1358
+ struct inode * inode ;
1359
+
1360
+ if (!OVL_FS (dentry -> d_sb )-> tmpfile )
1361
+ return - EOPNOTSUPP ;
1362
+
1363
+ err = ovl_want_write (dentry );
1364
+ if (err )
1365
+ return err ;
1366
+
1367
+ err = - ENOMEM ;
1368
+ inode = ovl_new_inode (dentry -> d_sb , mode , 0 );
1369
+ if (!inode )
1370
+ goto drop_write ;
1371
+
1372
+ inode_init_owner (& nop_mnt_idmap , inode , dir , mode );
1373
+ err = ovl_create_tmpfile (file , dentry , inode , inode -> i_mode );
1374
+ if (err )
1375
+ goto put_inode ;
1376
+
1377
+ /*
1378
+ * Check if the preallocated inode was actually used. Having something
1379
+ * else assigned to the dentry shouldn't happen as that would indicate
1380
+ * that the backing tmpfile "leaked" out of overlayfs.
1381
+ */
1382
+ err = - EIO ;
1383
+ if (WARN_ON (inode != d_inode (dentry )))
1384
+ goto put_realfile ;
1385
+
1386
+ /* inode reference was transferred to dentry */
1387
+ inode = NULL ;
1388
+ err = finish_open (file , dentry , ovl_dummy_open );
1389
+ put_realfile :
1390
+ /* Without FMODE_OPENED ->release() won't be called on @file */
1391
+ if (!(file -> f_mode & FMODE_OPENED ))
1392
+ fput (file -> private_data );
1393
+ put_inode :
1394
+ iput (inode );
1395
+ drop_write :
1396
+ ovl_drop_write (dentry );
1397
+ return err ;
1398
+ }
1399
+
1293
1400
const struct inode_operations ovl_dir_inode_operations = {
1294
1401
.lookup = ovl_lookup ,
1295
1402
.mkdir = ovl_mkdir ,
@@ -1310,4 +1417,5 @@ const struct inode_operations ovl_dir_inode_operations = {
1310
1417
.update_time = ovl_update_time ,
1311
1418
.fileattr_get = ovl_fileattr_get ,
1312
1419
.fileattr_set = ovl_fileattr_set ,
1420
+ .tmpfile = ovl_tmpfile ,
1313
1421
};
0 commit comments