0011-Fix-up-non-directory-creation-in-SGID-directories.patch 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Linus Torvalds <[email protected]>
  3. Date: Tue, 3 Jul 2018 17:10:19 -0700
  4. Subject: [PATCH] Fix up non-directory creation in SGID directories
  5. sgid directories have special semantics, making newly created files in
  6. the directory belong to the group of the directory, and newly created
  7. subdirectories will also become sgid. This is historically used for
  8. group-shared directories.
  9. But group directories writable by non-group members should not imply
  10. that such non-group members can magically join the group, so make sure
  11. to clear the sgid bit on non-directories for non-members (but remember
  12. that sgid without group execute means "mandatory locking", just to
  13. confuse things even more).
  14. Reported-by: Jann Horn <[email protected]>
  15. Cc: Andy Lutomirski <[email protected]>
  16. Cc: Al Viro <[email protected]>
  17. Signed-off-by: Linus Torvalds <[email protected]>
  18. (cherry picked from commit 0fa3ecd87848c9c93c2c828ef4c3a8ca36ce46c7)
  19. Signed-off-by: Stoiko Ivanov <[email protected]>
  20. ---
  21. fs/inode.c | 6 ++++++
  22. 1 file changed, 6 insertions(+)
  23. diff --git a/fs/inode.c b/fs/inode.c
  24. index 5c1138e9cac0..797b4cb3d20b 100644
  25. --- a/fs/inode.c
  26. +++ b/fs/inode.c
  27. @@ -2008,8 +2008,14 @@ void inode_init_owner(struct inode *inode, const struct inode *dir,
  28. inode->i_uid = current_fsuid();
  29. if (dir && dir->i_mode & S_ISGID) {
  30. inode->i_gid = dir->i_gid;
  31. +
  32. + /* Directories are special, and always inherit S_ISGID */
  33. if (S_ISDIR(mode))
  34. mode |= S_ISGID;
  35. + else if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP) &&
  36. + !in_group_p(inode->i_gid) &&
  37. + !capable_wrt_inode_uidgid(dir, CAP_FSETID))
  38. + mode &= ~S_ISGID;
  39. } else
  40. inode->i_gid = current_fsgid();
  41. inode->i_mode = mode;