linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: viro@zeniv.linux.org.uk
Cc: Casey Schaufler <casey@schaufler-ca.com>,
	Casey Schaufler <casey@schaufler-ca.com>,
	dhowells@redhat.com, raven@themaw.net,
	linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org,
	linux-block@vger.kernel.org, keyrings@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 01/10] security: Override creds in __fput() with last fputter's creds [ver #3]
Date: Thu, 06 Jun 2019 10:42:10 +0100	[thread overview]
Message-ID: <155981413016.17513.10540579988392555403.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <155981411940.17513.7137844619951358374.stgit@warthog.procyon.org.uk>

So that the LSM can see the credentials of the last process to do an fput()
on a file object when the file object is being dismantled, do the following
steps:

 (1) Cache the current credentials in file->f_fput_cred at the point the
     file object's reference count reaches zero.

 (2) In __fput(), use override_creds() to apply those credentials to the
     dismantling process.  This is necessary so that if we're dismantling a
     unix socket that has semi-passed fds still in it, their fputs will
     pick up the same credentials if they're reduced to zero at that point.

     Note that it's probably not strictly necessary to take an extra ref on
     the creds here (which override_creds() does).

 (3) Destroy the fput creds in file_free_rcu().

This additionally makes the creds available to:

	fsnotify
	eventpoll
	file locking
	->fasync, ->release file ops
	superblock destruction
	mountpoint destruction

This allows various notifications about object cleanups/destructions to
carry appropriate credentials for the LSM to approve/disapprove them based
on the process that caused them, even if indirectly.

Note that this means that someone looking at /proc/<pid>/fd/<n> may end up
being inadvertently noted as the subject of a cleanup message if the
process they're looking at croaks whilst they're looking at it.

Further, kernel services like nfsd and cachefiles may be seen as the
fputter and may not have a system credential.  In cachefiles's case, it may
appear that cachefilesd caused the notification.

Suggested-by: Casey Schaufler <casey@schaufler-ca.com>
Signed-off-by: David Howells <dhowells@redhat.com>
cc: Casey Schaufler <casey@schaufler-ca.com>
---

 fs/file_table.c    |   12 ++++++++++++
 include/linux/fs.h |    1 +
 2 files changed, 13 insertions(+)

diff --git a/fs/file_table.c b/fs/file_table.c
index 3f9c1b452c1d..9bf2be45b7f9 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -46,6 +46,7 @@ static void file_free_rcu(struct rcu_head *head)
 	struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
 
 	put_cred(f->f_cred);
+	put_cred(f->f_fput_cred);
 	kmem_cache_free(filp_cachep, f);
 }
 
@@ -252,6 +253,7 @@ struct file *alloc_file_clone(struct file *base, int flags,
  */
 static void __fput(struct file *file)
 {
+	const struct cred *saved_cred;
 	struct dentry *dentry = file->f_path.dentry;
 	struct vfsmount *mnt = file->f_path.mnt;
 	struct inode *inode = file->f_inode;
@@ -262,6 +264,12 @@ static void __fput(struct file *file)
 
 	might_sleep();
 
+	/* Set the creds of whoever triggered the last fput for the LSM.  Note
+	 * that this has to be made available to further fputs, say on fds
+	 * trapped in a unix socket.
+	 */
+	saved_cred = override_creds(file->f_fput_cred);
+
 	fsnotify_close(file);
 	/*
 	 * The function eventpoll_release() should be the first called
@@ -293,6 +301,8 @@ static void __fput(struct file *file)
 	if (unlikely(mode & FMODE_NEED_UNMOUNT))
 		dissolve_on_fput(mnt);
 	mntput(mnt);
+
+	revert_creds(saved_cred);
 out:
 	file_free(file);
 }
@@ -334,6 +344,7 @@ void fput_many(struct file *file, unsigned int refs)
 	if (atomic_long_sub_and_test(refs, &file->f_count)) {
 		struct task_struct *task = current;
 
+		file->f_fput_cred = get_current_cred();
 		if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
 			init_task_work(&file->f_u.fu_rcuhead, ____fput);
 			if (!task_work_add(task, &file->f_u.fu_rcuhead, true))
@@ -368,6 +379,7 @@ void __fput_sync(struct file *file)
 	if (atomic_long_dec_and_test(&file->f_count)) {
 		struct task_struct *task = current;
 		BUG_ON(!(task->flags & PF_KTHREAD));
+		file->f_fput_cred = get_current_cred();
 		__fput(file);
 	}
 }
diff --git a/include/linux/fs.h b/include/linux/fs.h
index f1c74596cd77..db05738b1951 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -943,6 +943,7 @@ struct file {
 	loff_t			f_pos;
 	struct fown_struct	f_owner;
 	const struct cred	*f_cred;
+	const struct cred	*f_fput_cred;	/* Who did the last fput() (for LSM) */
 	struct file_ra_state	f_ra;
 
 	u64			f_version;


  reply	other threads:[~2019-06-06  9:42 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-06  9:41 [RFC][PATCH 00/10] Mount, FS, Block and Keyrings notifications [ver #3] David Howells
2019-06-06  9:42 ` David Howells [this message]
2019-06-06 14:57   ` [PATCH 01/10] security: Override creds in __fput() with last fputter's creds " Andy Lutomirski
2019-06-06 15:06   ` David Howells
2019-06-06 17:18     ` Andy Lutomirski
2019-06-06 19:09       ` Casey Schaufler
2019-06-06 19:34         ` Andy Lutomirski
2019-06-06  9:42 ` [PATCH 02/10] General notification queue with user mmap()'able ring buffer " David Howells
2019-06-06  9:42 ` [PATCH 03/10] keys: Add a notification facility " David Howells
2019-06-06  9:42 ` [PATCH 04/10] vfs: Add a mount-notification " David Howells
2019-06-06  9:42 ` [PATCH 05/10] vfs: Add superblock notifications " David Howells
2019-06-06  9:42 ` [PATCH 06/10] fsinfo: Export superblock notification counter " David Howells
2019-06-06  9:43 ` [PATCH 07/10] Add a general, global device notification watch list " David Howells
2019-06-06  9:43 ` [PATCH 08/10] block: Add block layer notifications " David Howells
2019-06-06  9:43 ` [PATCH 09/10] usb: Add USB subsystem " David Howells
2019-06-06 14:24   ` Alan Stern
2019-06-06 14:33     ` Greg Kroah-Hartman
2019-06-06 14:55       ` Alan Stern
2019-06-06 15:31         ` Greg Kroah-Hartman
2019-06-07  6:40           ` Felipe Balbi
2019-06-07 14:01             ` Alan Stern
2019-06-11  6:28               ` Felipe Balbi
2019-06-11 13:53                 ` Alan Stern
2019-06-12  6:58                   ` Felipe Balbi
2019-06-06  9:43 ` [PATCH 10/10] Add sample notification program " David Howells
2019-06-06 21:21   ` Eugeniu Rosca
2019-06-06 22:52   ` David Howells
2019-06-07 14:37   ` David Howells
2019-06-06 12:32 ` [RFC][PATCH 00/10] Mount, FS, Block and Keyrings notifications " Stephen Smalley
2019-06-06 13:16 ` David Howells
2019-06-06 14:05   ` Stephen Smalley
2019-06-06 16:43     ` Casey Schaufler
2019-06-06 17:11       ` Andy Lutomirski
2019-06-06 18:33         ` Casey Schaufler
2019-06-06 18:51           ` Andy Lutomirski
2019-06-06 17:16       ` Stephen Smalley
2019-06-06 18:56         ` Casey Schaufler
2019-06-06 19:54           ` Andy Lutomirski
2019-06-06 21:17           ` David Howells
2019-06-06 21:54             ` Andy Lutomirski
2019-06-06 22:38             ` David Howells
2019-06-06 22:42               ` Andy Lutomirski
2019-06-06 22:50               ` David Howells
2019-06-06 14:34 ` Christian Brauner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=155981413016.17513.10540579988392555403.stgit@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=casey@schaufler-ca.com \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=raven@themaw.net \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).