@@ -1444,69 +1444,52 @@ int may_open(struct path *path, int acc_mode, int flag)
14441444 if (error )
14451445 return error ;
14461446
1447- error = ima_path_check (path , acc_mode ?
1448- acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC ) :
1449- ACC_MODE (flag ) & (MAY_READ | MAY_WRITE ),
1450- IMA_COUNT_UPDATE );
1451-
1452- if (error )
1453- return error ;
14541447 /*
14551448 * An append-only file must be opened in append mode for writing.
14561449 */
14571450 if (IS_APPEND (inode )) {
1458- error = - EPERM ;
14591451 if ((flag & FMODE_WRITE ) && !(flag & O_APPEND ))
1460- goto err_out ;
1452+ return - EPERM ;
14611453 if (flag & O_TRUNC )
1462- goto err_out ;
1454+ return - EPERM ;
14631455 }
14641456
14651457 /* O_NOATIME can only be set by the owner or superuser */
1466- if (flag & O_NOATIME )
1467- if (!is_owner_or_cap (inode )) {
1468- error = - EPERM ;
1469- goto err_out ;
1470- }
1458+ if (flag & O_NOATIME && !is_owner_or_cap (inode ))
1459+ return - EPERM ;
14711460
14721461 /*
14731462 * Ensure there are no outstanding leases on the file.
14741463 */
14751464 error = break_lease (inode , flag );
14761465 if (error )
1477- goto err_out ;
1478-
1479- if (flag & O_TRUNC ) {
1480- error = get_write_access (inode );
1481- if (error )
1482- goto err_out ;
1483-
1484- /*
1485- * Refuse to truncate files with mandatory locks held on them.
1486- */
1487- error = locks_verify_locked (inode );
1488- if (!error )
1489- error = security_path_truncate (path , 0 ,
1490- ATTR_MTIME |ATTR_CTIME |ATTR_OPEN );
1491- if (!error ) {
1492- vfs_dq_init (inode );
1466+ return error ;
14931467
1494- error = do_truncate (dentry , 0 ,
1495- ATTR_MTIME |ATTR_CTIME |ATTR_OPEN ,
1496- NULL );
1497- }
1498- put_write_access (inode );
1499- if (error )
1500- goto err_out ;
1501- } else
1502- if (flag & FMODE_WRITE )
1503- vfs_dq_init (inode );
1468+ return ima_path_check (path , acc_mode ?
1469+ acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC ) :
1470+ ACC_MODE (flag ) & (MAY_READ | MAY_WRITE ),
1471+ IMA_COUNT_UPDATE );
1472+ }
15041473
1505- return 0 ;
1506- err_out :
1507- ima_counts_put (path , acc_mode ?
1508- acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC ) :
1509- ACC_MODE (flag ) & (MAY_READ | MAY_WRITE ));
1474+ static int handle_truncate (struct path * path )
1475+ {
1476+ struct inode * inode = path -> dentry -> d_inode ;
1477+ int error = get_write_access (inode );
1478+ if (error )
1479+ return error ;
1480+ /*
1481+ * Refuse to truncate files with mandatory locks held on them.
1482+ */
1483+ error = locks_verify_locked (inode );
1484+ if (!error )
1485+ error = security_path_truncate (path , 0 ,
1486+ ATTR_MTIME |ATTR_CTIME |ATTR_OPEN );
1487+ if (!error ) {
1488+ error = do_truncate (path -> dentry , 0 ,
1489+ ATTR_MTIME |ATTR_CTIME |ATTR_OPEN ,
1490+ NULL );
1491+ }
1492+ put_write_access (inode );
15101493 return error ;
15111494}
15121495
@@ -1561,7 +1544,7 @@ static inline int open_to_namei_flags(int flag)
15611544 return flag ;
15621545}
15631546
1564- static int open_will_write_to_fs (int flag , struct inode * inode )
1547+ static int open_will_truncate (int flag , struct inode * inode )
15651548{
15661549 /*
15671550 * We'll never write to the fs underlying
@@ -1586,7 +1569,7 @@ struct file *do_filp_open(int dfd, const char *pathname,
15861569 struct path path , save ;
15871570 struct dentry * dir ;
15881571 int count = 0 ;
1589- int will_write ;
1572+ int will_truncate ;
15901573 int flag = open_to_namei_flags (open_flag );
15911574
15921575 /*
@@ -1752,28 +1735,48 @@ struct file *do_filp_open(int dfd, const char *pathname,
17521735 * be avoided. Taking this mnt write here
17531736 * ensures that (2) can not occur.
17541737 */
1755- will_write = open_will_write_to_fs (flag , nd .path .dentry -> d_inode );
1756- if (will_write ) {
1738+ will_truncate = open_will_truncate (flag , nd .path .dentry -> d_inode );
1739+ if (will_truncate ) {
17571740 error = mnt_want_write (nd .path .mnt );
17581741 if (error )
17591742 goto exit ;
17601743 }
17611744 error = may_open (& nd .path , acc_mode , flag );
17621745 if (error ) {
1763- if (will_write )
1746+ if (will_truncate )
17641747 mnt_drop_write (nd .path .mnt );
17651748 goto exit ;
17661749 }
17671750 filp = nameidata_to_filp (& nd , open_flag );
1768- if (IS_ERR (filp ))
1751+ if (IS_ERR (filp )) {
17691752 ima_counts_put (& nd .path ,
17701753 acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC ));
1754+ if (will_truncate )
1755+ mnt_drop_write (nd .path .mnt );
1756+ if (nd .root .mnt )
1757+ path_put (& nd .root );
1758+ return filp ;
1759+ }
1760+
1761+ if (acc_mode & MAY_WRITE )
1762+ vfs_dq_init (nd .path .dentry -> d_inode );
1763+
1764+ if (will_truncate ) {
1765+ error = handle_truncate (& nd .path );
1766+ if (error ) {
1767+ mnt_drop_write (nd .path .mnt );
1768+ fput (filp );
1769+ if (nd .root .mnt )
1770+ path_put (& nd .root );
1771+ return ERR_PTR (error );
1772+ }
1773+ }
17711774 /*
17721775 * It is now safe to drop the mnt write
17731776 * because the filp has had a write taken
17741777 * on its behalf.
17751778 */
1776- if (will_write )
1779+ if (will_truncate )
17771780 mnt_drop_write (nd .path .mnt );
17781781 if (nd .root .mnt )
17791782 path_put (& nd .root );
0 commit comments