Skip to content

Commit 6905732

Browse files
committed
Merge tag 'for_linus_stable' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4
Pull fscrypto fixes fromTed Ts'o: "Fix some brown-paper-bag bugs for fscrypto, including one one which allows a malicious user to set an encryption policy on an empty directory which they do not own" * tag 'for_linus_stable' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4: fscrypto: require write access to mount to set encryption policy fscrypto: only allow setting encryption policy on directories fscrypto: add authorization check for setting encryption policy
2 parents d0acc7d + ba63f23 commit 6905732

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

fs/crypto/policy.c

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/random.h>
1212
#include <linux/string.h>
1313
#include <linux/fscrypto.h>
14+
#include <linux/mount.h>
1415

1516
static int inode_has_encryption_context(struct inode *inode)
1617
{
@@ -92,26 +93,42 @@ static int create_encryption_context_from_policy(struct inode *inode,
9293
return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL);
9394
}
9495

95-
int fscrypt_process_policy(struct inode *inode,
96+
int fscrypt_process_policy(struct file *filp,
9697
const struct fscrypt_policy *policy)
9798
{
99+
struct inode *inode = file_inode(filp);
100+
int ret;
101+
102+
if (!inode_owner_or_capable(inode))
103+
return -EACCES;
104+
98105
if (policy->version != 0)
99106
return -EINVAL;
100107

108+
ret = mnt_want_write_file(filp);
109+
if (ret)
110+
return ret;
111+
101112
if (!inode_has_encryption_context(inode)) {
102-
if (!inode->i_sb->s_cop->empty_dir)
103-
return -EOPNOTSUPP;
104-
if (!inode->i_sb->s_cop->empty_dir(inode))
105-
return -ENOTEMPTY;
106-
return create_encryption_context_from_policy(inode, policy);
113+
if (!S_ISDIR(inode->i_mode))
114+
ret = -EINVAL;
115+
else if (!inode->i_sb->s_cop->empty_dir)
116+
ret = -EOPNOTSUPP;
117+
else if (!inode->i_sb->s_cop->empty_dir(inode))
118+
ret = -ENOTEMPTY;
119+
else
120+
ret = create_encryption_context_from_policy(inode,
121+
policy);
122+
} else if (!is_encryption_context_consistent_with_policy(inode,
123+
policy)) {
124+
printk(KERN_WARNING
125+
"%s: Policy inconsistent with encryption context\n",
126+
__func__);
127+
ret = -EINVAL;
107128
}
108129

109-
if (is_encryption_context_consistent_with_policy(inode, policy))
110-
return 0;
111-
112-
printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
113-
__func__);
114-
return -EINVAL;
130+
mnt_drop_write_file(filp);
131+
return ret;
115132
}
116133
EXPORT_SYMBOL(fscrypt_process_policy);
117134

fs/ext4/ioctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
776776
(struct fscrypt_policy __user *)arg,
777777
sizeof(policy)))
778778
return -EFAULT;
779-
return fscrypt_process_policy(inode, &policy);
779+
return fscrypt_process_policy(filp, &policy);
780780
#else
781781
return -EOPNOTSUPP;
782782
#endif

fs/f2fs/file.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,21 +1757,14 @@ static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
17571757
{
17581758
struct fscrypt_policy policy;
17591759
struct inode *inode = file_inode(filp);
1760-
int ret;
17611760

17621761
if (copy_from_user(&policy, (struct fscrypt_policy __user *)arg,
17631762
sizeof(policy)))
17641763
return -EFAULT;
17651764

1766-
ret = mnt_want_write_file(filp);
1767-
if (ret)
1768-
return ret;
1769-
17701765
f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1771-
ret = fscrypt_process_policy(inode, &policy);
17721766

1773-
mnt_drop_write_file(filp);
1774-
return ret;
1767+
return fscrypt_process_policy(filp, &policy);
17751768
}
17761769

17771770
static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)

include/linux/fscrypto.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,7 @@ extern void fscrypt_restore_control_page(struct page *);
274274
extern int fscrypt_zeroout_range(struct inode *, pgoff_t, sector_t,
275275
unsigned int);
276276
/* policy.c */
277-
extern int fscrypt_process_policy(struct inode *,
278-
const struct fscrypt_policy *);
277+
extern int fscrypt_process_policy(struct file *, const struct fscrypt_policy *);
279278
extern int fscrypt_get_policy(struct inode *, struct fscrypt_policy *);
280279
extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
281280
extern int fscrypt_inherit_context(struct inode *, struct inode *,
@@ -345,7 +344,7 @@ static inline int fscrypt_notsupp_zeroout_range(struct inode *i, pgoff_t p,
345344
}
346345

347346
/* policy.c */
348-
static inline int fscrypt_notsupp_process_policy(struct inode *i,
347+
static inline int fscrypt_notsupp_process_policy(struct file *f,
349348
const struct fscrypt_policy *p)
350349
{
351350
return -EOPNOTSUPP;

0 commit comments

Comments
 (0)