diff mbox series

[v16,2/4] overlayfs: handle XATTR_NOSECURITY flag for get xattr method

Message ID 20201019115239.2732422-3-salyzyn@android.com
State New
Headers show
Series overlayfs override_creds=off & nested get xattr fix | expand

Commit Message

Mark Salyzyn Oct. 19, 2020, 11:52 a.m. UTC
Because of the overlayfs getxattr recursion, the incoming inode fails
to update the selinux sid resulting in avc denials being reported
against a target context of u:object_r:unlabeled:s0.

Solution is to respond to the XATTR_NOSECURITY flag in get xattr
method that calls the __vfs_getxattr handler instead so that the
context can be read in, rather than being denied with an -EACCES
when vfs_getxattr handler is called.

For the use case where access is to be blocked by the security layer.

The path then would be security(dentry) ->
__vfs_getxattr({dentry...XATTR_NOSECURITY}) ->
handler->get({dentry...XATTR_NOSECURITY}) ->
__vfs_getxattr({realdentry...XATTR_NOSECURITY}) ->
lower_handler->get({realdentry...XATTR_NOSECURITY}) which
would report back through the chain data and success as expected,
the logging security layer at the top would have the data to
determine the access permissions and report back to the logs and
the caller that the target context was blocked.

For selinux this would solve the cosmetic issue of the selinux log
and allow audit2allow to correctly report the rule needed to address
the access problem.

Check impure, opaque, origin & meta xattr with no sepolicy audit
(using __vfs_getxattr) since these operations are internal to
overlayfs operations and do not disclose any data.  This became
an issue for credential override off since sys_admin would have
been required by the caller; whereas would have been inherently
present for the creator since it performed the mount.

This is a change in operations since we do not check in the new
ovl_do_getxattr function if the credential override is off or not.
Reasoning is that the sepolicy check is unnecessary overhead,
especially since the check can be expensive.

Because for override credentials off, this affects _everyone_ that
underneath performs private xattr calls without the appropriate
sepolicy permissions and sys_admin capability.  Providing blanket
support for sys_admin would be bad for all possible callers.

For the override credentials on, this will affect only the mounter,
should it lack sepolicy permissions. Not considered a security
problem since mounting by definition has sys_admin capabilities,
but sepolicy contexts would still need to be crafted.

It should be noted that there is precedence, __vfs_getxattr is used
in other filesystems for their own internal trusted xattr management.

Signed-off-by: Mark Salyzyn <salyzyn@android.com>
To: linux-fsdevel@vger.kernel.org
To: linux-unionfs@vger.kernel.org
Cc: Stephen Smalley <sds@tycho.nsa.gov>
Cc: linux-kernel@vger.kernel.org
Cc: linux-security-module@vger.kernel.org
Cc: kernel-team@android.com
Cc: Miklos Szeredi <miklos@szeredi.hu>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Amir Goldstein <amir73il@gmail.com>
Cc: linux-doc@vger.kernel.org
Cc: John Stultz <john.stultz@linaro.org>

v16 - rebase and merge internal getxattr operations patch

v15 - revert to v13 because xattr_gs_args rejected.

v14 - rebase to use xattr_gs_args.

v13 - rebase to use __vfs_getxattr flags option.

v12 - Added back to patch series as get xattr with flag option.

v11 - Squashed out of patch series and replaced with per-thread flag
      solution.

v10 - Added to patch series as __get xattr method.
---
 fs/overlayfs/inode.c     | 5 +++--
 fs/overlayfs/overlayfs.h | 4 ++--
 fs/overlayfs/super.c     | 4 ++--
 3 files changed, 7 insertions(+), 6 deletions(-)

Comments

kernel test robot Oct. 19, 2020, 2:11 p.m. UTC | #1
Hi Mark,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on miklos-vfs/overlayfs-next]
[also build test ERROR on linus/master next-20201016]
[cannot apply to ext4/dev security/next-testing v5.9]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Mark-Salyzyn/overlayfs-override_creds-off-nested-get-xattr-fix/20201019-195516
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs.git overlayfs-next
config: i386-randconfig-c001-20201019 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/1cb141c810d73ddaf9ef64889ed4d4fed81bb453
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Mark-Salyzyn/overlayfs-override_creds-off-nested-get-xattr-fix/20201019-195516
        git checkout 1cb141c810d73ddaf9ef64889ed4d4fed81bb453
        # save the attached .config to linux build tree
        make W=1 ARCH=i386 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All error/warnings (new ones prefixed by >>):

   In file included from fs/overlayfs/super.c:18:
   fs/overlayfs/overlayfs.h: In function 'ovl_do_getxattr':
>> fs/overlayfs/overlayfs.h:187:32: error: passing argument 2 of '__vfs_getxattr' from incompatible pointer type [-Werror=incompatible-pointer-types]
     187 |  return __vfs_getxattr(dentry, name, value, size, XATTR_NOSECURITY);
         |                                ^~~~
         |                                |
         |                                const char *
   In file included from fs/overlayfs/super.c:10:
   include/linux/xattr.h:50:61: note: expected 'struct inode *' but argument is of type 'const char *'
      50 | ssize_t __vfs_getxattr(struct dentry *dentry, struct inode *inode,
         |                                               ~~~~~~~~~~~~~~^~~~~
   In file included from fs/overlayfs/super.c:18:
>> fs/overlayfs/overlayfs.h:187:45: warning: passing argument 4 of '__vfs_getxattr' makes pointer from integer without a cast [-Wint-conversion]
     187 |  return __vfs_getxattr(dentry, name, value, size, XATTR_NOSECURITY);
         |                                             ^~~~
         |                                             |
         |                                             size_t {aka unsigned int}
   In file included from fs/overlayfs/super.c:10:
   include/linux/xattr.h:51:34: note: expected 'void *' but argument is of type 'size_t' {aka 'unsigned int'}
      51 |          const char *name, void *buffer, size_t size, int flags);
         |                            ~~~~~~^~~~~~
   In file included from fs/overlayfs/super.c:18:
>> fs/overlayfs/overlayfs.h:187:9: error: too few arguments to function '__vfs_getxattr'
     187 |  return __vfs_getxattr(dentry, name, value, size, XATTR_NOSECURITY);
         |         ^~~~~~~~~~~~~~
   In file included from fs/overlayfs/super.c:10:
   include/linux/xattr.h:50:9: note: declared here
      50 | ssize_t __vfs_getxattr(struct dentry *dentry, struct inode *inode,
         |         ^~~~~~~~~~~~~~
   cc1: some warnings being treated as errors
--
   In file included from fs/overlayfs/namei.c:15:
   fs/overlayfs/overlayfs.h: In function 'ovl_do_getxattr':
>> fs/overlayfs/overlayfs.h:187:32: error: passing argument 2 of '__vfs_getxattr' from incompatible pointer type [-Werror=incompatible-pointer-types]
     187 |  return __vfs_getxattr(dentry, name, value, size, XATTR_NOSECURITY);
         |                                ^~~~
         |                                |
         |                                const char *
   In file included from fs/overlayfs/namei.c:11:
   include/linux/xattr.h:50:61: note: expected 'struct inode *' but argument is of type 'const char *'
      50 | ssize_t __vfs_getxattr(struct dentry *dentry, struct inode *inode,
         |                                               ~~~~~~~~~~~~~~^~~~~
   In file included from fs/overlayfs/namei.c:15:
>> fs/overlayfs/overlayfs.h:187:45: warning: passing argument 4 of '__vfs_getxattr' makes pointer from integer without a cast [-Wint-conversion]
     187 |  return __vfs_getxattr(dentry, name, value, size, XATTR_NOSECURITY);
         |                                             ^~~~
         |                                             |
         |                                             size_t {aka unsigned int}
   In file included from fs/overlayfs/namei.c:11:
   include/linux/xattr.h:51:34: note: expected 'void *' but argument is of type 'size_t' {aka 'unsigned int'}
      51 |          const char *name, void *buffer, size_t size, int flags);
         |                            ~~~~~~^~~~~~
   In file included from fs/overlayfs/namei.c:15:
>> fs/overlayfs/overlayfs.h:187:9: error: too few arguments to function '__vfs_getxattr'
     187 |  return __vfs_getxattr(dentry, name, value, size, XATTR_NOSECURITY);
         |         ^~~~~~~~~~~~~~
   In file included from fs/overlayfs/namei.c:11:
   include/linux/xattr.h:50:9: note: declared here
      50 | ssize_t __vfs_getxattr(struct dentry *dentry, struct inode *inode,
         |         ^~~~~~~~~~~~~~
   In file included from fs/overlayfs/namei.c:15:
>> fs/overlayfs/overlayfs.h:188:1: warning: control reaches end of non-void function [-Wreturn-type]
     188 | }
         | ^
   cc1: some warnings being treated as errors

vim +/__vfs_getxattr +187 fs/overlayfs/overlayfs.h

   181	
   182	static inline ssize_t ovl_do_getxattr(struct ovl_fs *ofs, struct dentry *dentry,
   183					      enum ovl_xattr ox, void *value,
   184					      size_t size)
   185	{
   186		const char *name = ovl_xattr(ofs, ox);
 > 187		return __vfs_getxattr(dentry, name, value, size, XATTR_NOSECURITY);
 > 188	}
   189	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
kernel test robot Oct. 19, 2020, 5:13 p.m. UTC | #2
Hi Mark,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on miklos-vfs/overlayfs-next]
[also build test ERROR on linus/master next-20201016]
[cannot apply to ext4/dev security/next-testing v5.9]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Mark-Salyzyn/overlayfs-override_creds-off-nested-get-xattr-fix/20201019-195516
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs.git overlayfs-next
config: x86_64-randconfig-a016-20201019 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 094e9f4779eb9b5c6a49014f2f80b8cbb833572f)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install x86_64 cross compiling tool for clang build
        # apt-get install binutils-x86-64-linux-gnu
        # https://github.com/0day-ci/linux/commit/1cb141c810d73ddaf9ef64889ed4d4fed81bb453
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Mark-Salyzyn/overlayfs-override_creds-off-nested-get-xattr-fix/20201019-195516
        git checkout 1cb141c810d73ddaf9ef64889ed4d4fed81bb453
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   In file included from fs/overlayfs/super.c:18:
>> fs/overlayfs/overlayfs.h:187:67: error: too few arguments to function call, expected 6, have 5
           return __vfs_getxattr(dentry, name, value, size, XATTR_NOSECURITY);
                  ~~~~~~~~~~~~~~                                            ^
   include/linux/xattr.h:50:9: note: '__vfs_getxattr' declared here
   ssize_t __vfs_getxattr(struct dentry *dentry, struct inode *inode,
           ^
   1 error generated.

vim +187 fs/overlayfs/overlayfs.h

   181	
   182	static inline ssize_t ovl_do_getxattr(struct ovl_fs *ofs, struct dentry *dentry,
   183					      enum ovl_xattr ox, void *value,
   184					      size_t size)
   185	{
   186		const char *name = ovl_xattr(ofs, ox);
 > 187		return __vfs_getxattr(dentry, name, value, size, XATTR_NOSECURITY);
   188	}
   189	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/fs/overlayfs/inode.c b/fs/overlayfs/inode.c
index b584dca845ba..2b14291beb86 100644
--- a/fs/overlayfs/inode.c
+++ b/fs/overlayfs/inode.c
@@ -378,7 +378,7 @@  int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
 }
 
 int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
-		  void *value, size_t size)
+		  void *value, size_t size, int flags)
 {
 	ssize_t res;
 	const struct cred *old_cred;
@@ -386,7 +386,8 @@  int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
 		ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
 
 	old_cred = ovl_override_creds(dentry->d_sb);
-	res = vfs_getxattr(realdentry, name, value, size);
+	res = __vfs_getxattr(realdentry, d_inode(realdentry), name,
+			     value, size, flags);
 	revert_creds(old_cred);
 	return res;
 }
diff --git a/fs/overlayfs/overlayfs.h b/fs/overlayfs/overlayfs.h
index f8880aa2ba0e..3033f0c4de5f 100644
--- a/fs/overlayfs/overlayfs.h
+++ b/fs/overlayfs/overlayfs.h
@@ -184,7 +184,7 @@  static inline ssize_t ovl_do_getxattr(struct ovl_fs *ofs, struct dentry *dentry,
 				      size_t size)
 {
 	const char *name = ovl_xattr(ofs, ox);
-	return vfs_getxattr(dentry, name, value, size);
+	return __vfs_getxattr(dentry, name, value, size, XATTR_NOSECURITY);
 }
 
 static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
@@ -439,7 +439,7 @@  int ovl_permission(struct inode *inode, int mask);
 int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
 		  const void *value, size_t size, int flags);
 int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
-		  void *value, size_t size);
+		  void *value, size_t size, int flags);
 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
 struct posix_acl *ovl_get_acl(struct inode *inode, int type);
 int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags);
diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c
index f41353ba1e68..d447958badc2 100644
--- a/fs/overlayfs/super.c
+++ b/fs/overlayfs/super.c
@@ -930,7 +930,7 @@  ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
 			struct dentry *dentry, struct inode *inode,
 			const char *name, void *buffer, size_t size, int flags)
 {
-	return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
+	return ovl_xattr_get(dentry, inode, handler->name, buffer, size, flags);
 }
 
 static int __maybe_unused
@@ -1012,7 +1012,7 @@  static int ovl_other_xattr_get(const struct xattr_handler *handler,
 			       const char *name, void *buffer, size_t size,
 			       int flags)
 {
-	return ovl_xattr_get(dentry, inode, name, buffer, size);
+	return ovl_xattr_get(dentry, inode, name, buffer, size, flags);
 }
 
 static int ovl_other_xattr_set(const struct xattr_handler *handler,