2017-03-08 12:13:04

by Chao Yu

[permalink] [raw]
Subject: [PATCH] f2fs: don't allow rename unencrypted file to encrypted directory

In commit d9cdc9033181 ("ext4 crypto: enforce context consistency") we
declared that:

2) All files or directories in a directory must be protected using the
same key as their containing directory.

But in f2fs_cross_rename there is a vulnerability that allow to cross
rename unencrypted file into encrypted directory, it needs to be refused.

Signed-off-by: Chao Yu <[email protected]>
---
fs/f2fs/namei.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
index 25c073f6c7d4..8de684b84cb9 100644
--- a/fs/f2fs/namei.c
+++ b/fs/f2fs/namei.c
@@ -855,6 +855,10 @@ static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
!fscrypt_has_encryption_key(new_dir)))
return -ENOKEY;

+ if (f2fs_encrypted_inode(old_dir) && !f2fs_encrypted_inode(new_inode) ||
+ f2fs_encrypted_inode(new_dir) && !f2fs_encrypted_inode(old_inode))
+ return -EPERM;
+
if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
(old_dir != new_dir) &&
(!fscrypt_has_permitted_context(new_dir, old_inode) ||
--
2.8.2.295.g3f1c1d0


2017-03-08 15:26:09

by Kinglong Mee

[permalink] [raw]
Subject: Re: [f2fs-dev] [PATCH] f2fs: don't allow rename unencrypted file to encrypted directory

On 3/8/2017 20:08, Chao Yu wrote:
> In commit d9cdc9033181 ("ext4 crypto: enforce context consistency") we
> declared that:
>
> 2) All files or directories in a directory must be protected using the
> same key as their containing directory.
>
> But in f2fs_cross_rename there is a vulnerability that allow to cross
> rename unencrypted file into encrypted directory, it needs to be refused.

fscrypt_has_permitted_context has do the checking as this patch,

168 /* no restrictions if the parent directory is not encrypted */
169 if (!parent->i_sb->s_cop->is_encrypted(parent))
170 return 1;
171 /* if the child directory is not encrypted, this is always a problem */
172 if (!parent->i_sb->s_cop->is_encrypted(child))
173 return 0;

So, the cross rename unencrypted file into encrypted directory is permitted right now.

I have a encrypted directory "ncry", "new" is unencrypted file.

[root@nfstestnic f2fs]# renameat2 -x encry/hello new
Operation not permitted
[root@nfstestnic f2fs]# renameat2 -x encry/hello new
Operation not permitted

How do you test it?

thanks,
Kinglong Mee
>
> Signed-off-by: Chao Yu <[email protected]>
> ---
> fs/f2fs/namei.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
> index 25c073f6c7d4..8de684b84cb9 100644
> --- a/fs/f2fs/namei.c
> +++ b/fs/f2fs/namei.c
> @@ -855,6 +855,10 @@ static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
> !fscrypt_has_encryption_key(new_dir)))
> return -ENOKEY;
>
> + if (f2fs_encrypted_inode(old_dir) && !f2fs_encrypted_inode(new_inode) ||
> + f2fs_encrypted_inode(new_dir) && !f2fs_encrypted_inode(old_inode))
> + return -EPERM;
> +
> if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
> (old_dir != new_dir) &&
> (!fscrypt_has_permitted_context(new_dir, old_inode) ||
>

2017-03-09 01:33:30

by Chao Yu

[permalink] [raw]
Subject: Re: [f2fs-dev] [PATCH] f2fs: don't allow rename unencrypted file to encrypted directory

On 2017/3/8 21:35, Kinglong Mee wrote:
> On 3/8/2017 20:08, Chao Yu wrote:
>> In commit d9cdc9033181 ("ext4 crypto: enforce context consistency") we
>> declared that:
>>
>> 2) All files or directories in a directory must be protected using the
>> same key as their containing directory.
>>
>> But in f2fs_cross_rename there is a vulnerability that allow to cross
>> rename unencrypted file into encrypted directory, it needs to be refused.
>
> fscrypt_has_permitted_context has do the checking as this patch,
>
> 168 /* no restrictions if the parent directory is not encrypted */
> 169 if (!parent->i_sb->s_cop->is_encrypted(parent))
> 170 return 1;
> 171 /* if the child directory is not encrypted, this is always a problem */
> 172 if (!parent->i_sb->s_cop->is_encrypted(child))
> 173 return 0;
>
> So, the cross rename unencrypted file into encrypted directory is permitted right now.
>
> I have a encrypted directory "ncry", "new" is unencrypted file.
>
> [root@nfstestnic f2fs]# renameat2 -x encry/hello new
> Operation not permitted
> [root@nfstestnic f2fs]# renameat2 -x encry/hello new
> Operation not permitted

I've tried this, the same result as yours, so this patch is wrong, please ignore
it, sorry about the noise.

Thanks,

>
> How do you test it?
>
> thanks,
> Kinglong Mee
>>
>> Signed-off-by: Chao Yu <[email protected]>
>> ---
>> fs/f2fs/namei.c | 4 ++++
>> 1 file changed, 4 insertions(+)
>>
>> diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
>> index 25c073f6c7d4..8de684b84cb9 100644
>> --- a/fs/f2fs/namei.c
>> +++ b/fs/f2fs/namei.c
>> @@ -855,6 +855,10 @@ static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
>> !fscrypt_has_encryption_key(new_dir)))
>> return -ENOKEY;
>>
>> + if (f2fs_encrypted_inode(old_dir) && !f2fs_encrypted_inode(new_inode) ||
>> + f2fs_encrypted_inode(new_dir) && !f2fs_encrypted_inode(old_inode))
>> + return -EPERM;
>> +
>> if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
>> (old_dir != new_dir) &&
>> (!fscrypt_has_permitted_context(new_dir, old_inode) ||
>>
>
> .
>

2017-03-10 21:50:07

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] f2fs: don't allow rename unencrypted file to encrypted directory

Hi Chao,

[auto build test WARNING on f2fs/dev]
[also build test WARNING on v4.11-rc1 next-20170309]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Chao-Yu/f2fs-don-t-allow-rename-unencrypted-file-to-encrypted-directory/20170311-052705
base: https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git dev
config: i386-randconfig-x005-201710 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:
# save the attached .config to linux build tree
make ARCH=i386

All warnings (new ones prefixed by >>):

fs/f2fs/namei.c: In function 'f2fs_cross_rename':
>> fs/f2fs/namei.c:858:36: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
if (f2fs_encrypted_inode(old_dir) && !f2fs_encrypted_inode(new_inode) ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +858 fs/f2fs/namei.c

842 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
843 struct inode *old_inode = d_inode(old_dentry);
844 struct inode *new_inode = d_inode(new_dentry);
845 struct page *old_dir_page, *new_dir_page;
846 struct page *old_page, *new_page;
847 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
848 struct f2fs_dir_entry *old_entry, *new_entry;
849 int old_nlink = 0, new_nlink = 0;
850 int err = -ENOENT;
851
852 if ((f2fs_encrypted_inode(old_dir) &&
853 !fscrypt_has_encryption_key(old_dir)) ||
854 (f2fs_encrypted_inode(new_dir) &&
855 !fscrypt_has_encryption_key(new_dir)))
856 return -ENOKEY;
857
> 858 if (f2fs_encrypted_inode(old_dir) && !f2fs_encrypted_inode(new_inode) ||
859 f2fs_encrypted_inode(new_dir) && !f2fs_encrypted_inode(old_inode))
860 return -EPERM;
861
862 if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
863 (old_dir != new_dir) &&
864 (!fscrypt_has_permitted_context(new_dir, old_inode) ||
865 !fscrypt_has_permitted_context(old_dir, new_inode)))
866 return -EPERM;

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation


Attachments:
(No filename) (2.23 kB)
.config.gz (25.67 kB)
Download all attachments

2017-03-10 22:15:10

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] f2fs: don't allow rename unencrypted file to encrypted directory

Hi Chao,

[auto build test WARNING on f2fs/dev]
[also build test WARNING on v4.11-rc1 next-20170309]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Chao-Yu/f2fs-don-t-allow-rename-unencrypted-file-to-encrypted-directory/20170311-052705
base: https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git dev
config: x86_64-randconfig-n0-03110505 (attached as .config)
compiler: gcc-4.8 (Debian 4.8.4-1) 4.8.4
reproduce:
# save the attached .config to linux build tree
make ARCH=x86_64

All warnings (new ones prefixed by >>):

In file included from include/linux/linkage.h:4:0,
from include/linux/fs.h:4,
from fs/f2fs/namei.c:11:
fs/f2fs/namei.c: In function 'f2fs_cross_rename':
fs/f2fs/namei.c:858:36: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
if (f2fs_encrypted_inode(old_dir) && !f2fs_encrypted_inode(new_inode) ||
^
include/linux/compiler.h:160:30: note: in definition of macro '__trace_if'
if (__builtin_constant_p(!!(cond)) ? !!(cond) : \
^
>> fs/f2fs/namei.c:858:2: note: in expansion of macro 'if'
if (f2fs_encrypted_inode(old_dir) && !f2fs_encrypted_inode(new_inode) ||
^
fs/f2fs/namei.c:858:36: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
if (f2fs_encrypted_inode(old_dir) && !f2fs_encrypted_inode(new_inode) ||
^
include/linux/compiler.h:160:42: note: in definition of macro '__trace_if'
if (__builtin_constant_p(!!(cond)) ? !!(cond) : \
^
>> fs/f2fs/namei.c:858:2: note: in expansion of macro 'if'
if (f2fs_encrypted_inode(old_dir) && !f2fs_encrypted_inode(new_inode) ||
^
fs/f2fs/namei.c:858:36: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
if (f2fs_encrypted_inode(old_dir) && !f2fs_encrypted_inode(new_inode) ||
^
include/linux/compiler.h:171:16: note: in definition of macro '__trace_if'
______r = !!(cond); \
^
>> fs/f2fs/namei.c:858:2: note: in expansion of macro 'if'
if (f2fs_encrypted_inode(old_dir) && !f2fs_encrypted_inode(new_inode) ||
^

vim +/if +858 fs/f2fs/namei.c

842 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
843 struct inode *old_inode = d_inode(old_dentry);
844 struct inode *new_inode = d_inode(new_dentry);
845 struct page *old_dir_page, *new_dir_page;
846 struct page *old_page, *new_page;
847 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
848 struct f2fs_dir_entry *old_entry, *new_entry;
849 int old_nlink = 0, new_nlink = 0;
850 int err = -ENOENT;
851
852 if ((f2fs_encrypted_inode(old_dir) &&
853 !fscrypt_has_encryption_key(old_dir)) ||
854 (f2fs_encrypted_inode(new_dir) &&
855 !fscrypt_has_encryption_key(new_dir)))
856 return -ENOKEY;
857
> 858 if (f2fs_encrypted_inode(old_dir) && !f2fs_encrypted_inode(new_inode) ||
859 f2fs_encrypted_inode(new_dir) && !f2fs_encrypted_inode(old_inode))
860 return -EPERM;
861
862 if ((f2fs_encrypted_inode(old_dir) || f2fs_encrypted_inode(new_dir)) &&
863 (old_dir != new_dir) &&
864 (!fscrypt_has_permitted_context(new_dir, old_inode) ||
865 !fscrypt_has_permitted_context(old_dir, new_inode)))
866 return -EPERM;

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation


Attachments:
(No filename) (3.66 kB)
.config.gz (26.12 kB)
Download all attachments