linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Eric W. Biederman" <ebiederm@xmission.com>
To: Andrew Morton <akpm@osdl.org>
Cc: <linux-kernel@vger.kernel.org>, <containers@lists.osdl.org>,
	Oleg Nesterov <oleg@tv-sign.ru>,
	"Eric W. Biederman" <ebiederm@xmission.com>
Subject: [PATCH 7/7] file: Modify struct fown_struct to use a struct pid.
Date: Tue, 15 Aug 2006 12:23:12 -0600	[thread overview]
Message-ID: <11556661943487-git-send-email-ebiederm@xmission.com> (raw)
In-Reply-To: <m1k65997xk.fsf@ebiederm.dsl.xmission.com>

File handles can be requested to send sigio and sigurg
to processes.   By tracking the destination processes
using struct pid instead of pid_t we make the interface
safe from all potential pid wrap around problems.

Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
---
 drivers/net/tun.c  |    2 +
 fs/dnotify.c       |    2 +
 fs/fcntl.c         |   78 +++++++++++++++++++++++++++++++++-------------------
 fs/file_table.c    |    1 +
 fs/locks.c         |    2 +
 include/linux/fs.h |    5 +++
 kernel/futex.c     |    2 +
 net/socket.c       |    2 +
 8 files changed, 60 insertions(+), 34 deletions(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 9ab8ca6..c78d79c 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -697,7 +697,7 @@ static int tun_chr_fasync(int fd, struct
 		return ret; 
  
 	if (on) {
-		ret = f_setown(file, current->pid, 0);
+		ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
 		if (ret)
 			return ret;
 		tun->flags |= TUN_FASYNC;
diff --git a/fs/dnotify.c b/fs/dnotify.c
index f932591..2b0442d 100644
--- a/fs/dnotify.c
+++ b/fs/dnotify.c
@@ -92,7 +92,7 @@ int fcntl_dirnotify(int fd, struct file 
 		prev = &odn->dn_next;
 	}
 
-	error = f_setown(filp, current->pid, 0);
+	error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
 	if (error)
 		goto out_free;
 
diff --git a/fs/fcntl.c b/fs/fcntl.c
index b43d821..55d387c 100644
--- a/fs/fcntl.c
+++ b/fs/fcntl.c
@@ -250,19 +250,21 @@ static int setfl(int fd, struct file * f
 	return error;
 }
 
-static void f_modown(struct file *filp, unsigned long pid,
+static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
                      uid_t uid, uid_t euid, int force)
 {
 	write_lock_irq(&filp->f_owner.lock);
 	if (force || !filp->f_owner.pid) {
-		filp->f_owner.pid = pid;
+		put_pid(filp->f_owner.pid);
+		filp->f_owner.pid = get_pid(pid);
+		filp->f_owner.pid_type = type;
 		filp->f_owner.uid = uid;
 		filp->f_owner.euid = euid;
 	}
 	write_unlock_irq(&filp->f_owner.lock);
 }
 
-int f_setown(struct file *filp, unsigned long arg, int force)
+int __f_setown(struct file *filp, struct pid *pid, enum pid_type type, int force)
 {
 	int err;
 	
@@ -270,15 +272,44 @@ int f_setown(struct file *filp, unsigned
 	if (err)
 		return err;
 
-	f_modown(filp, arg, current->uid, current->euid, force);
+	f_modown(filp, pid, type, current->uid, current->euid, force);
 	return 0;
 }
 
+EXPORT_SYMBOL(__f_setown);
+
+int f_setown(struct file *filp, unsigned long arg, int force)
+{
+	enum pid_type type;
+	struct pid *pid;
+	int who = arg;
+	int result;
+	type = PIDTYPE_PID;
+	if (who < 0) {
+		type = PIDTYPE_PGID;
+		who = -who;
+	}
+	rcu_read_lock();
+	pid = find_pid(who);
+	result = __f_setown(filp, pid, type, force);
+	rcu_read_unlock();
+	return result;
+}
+
 EXPORT_SYMBOL(f_setown);
 
 void f_delown(struct file *filp)
 {
-	f_modown(filp, 0, 0, 0, 1);
+	f_modown(filp, NULL, PIDTYPE_PID, 0, 0, 1);
+}
+
+pid_t f_getown(struct file *filp)
+{
+	pid_t pid;
+	pid = pid_nr(filp->f_owner.pid);
+	if (sock->file->f_owner.pid_type == PIDTYPE_PGID)
+		pid = -pid;
+	return pid;
 }
 
 static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
@@ -319,7 +350,7 @@ static long do_fcntl(int fd, unsigned in
 		 * current syscall conventions, the only way
 		 * to fix this will be in libc.
 		 */
-		err = filp->f_owner.pid;
+		err = f_getown(filp);
 		force_successful_syscall_return();
 		break;
 	case F_SETOWN:
@@ -470,24 +501,19 @@ static void send_sigio_to_task(struct ta
 void send_sigio(struct fown_struct *fown, int fd, int band)
 {
 	struct task_struct *p;
-	int pid;
+	enum pid_type type;
+	struct pid *pid;
 	
 	read_lock(&fown->lock);
+	type = fown->pid_type;
 	pid = fown->pid;
 	if (!pid)
 		goto out_unlock_fown;
 	
 	read_lock(&tasklist_lock);
-	if (pid > 0) {
-		p = find_task_by_pid(pid);
-		if (p) {
-			send_sigio_to_task(p, fown, fd, band);
-		}
-	} else {
-		do_each_task_pid(-pid, PIDTYPE_PGID, p) {
-			send_sigio_to_task(p, fown, fd, band);
-		} while_each_task_pid(-pid, PIDTYPE_PGID, p);
-	}
+	do_each_pid_task(pid, type, p) {
+		send_sigio_to_task(p, fown, fd, band);
+	} while_each_pid_task(pid, type, p);
 	read_unlock(&tasklist_lock);
  out_unlock_fown:
 	read_unlock(&fown->lock);
@@ -503,9 +529,12 @@ static void send_sigurg_to_task(struct t
 int send_sigurg(struct fown_struct *fown)
 {
 	struct task_struct *p;
-	int pid, ret = 0;
+	enum pid_type type;
+	struct pid *pid;
+	int ret = 0;
 	
 	read_lock(&fown->lock);
+	type = fown->pid_type;
 	pid = fown->pid;
 	if (!pid)
 		goto out_unlock_fown;
@@ -513,16 +542,9 @@ int send_sigurg(struct fown_struct *fown
 	ret = 1;
 	
 	read_lock(&tasklist_lock);
-	if (pid > 0) {
-		p = find_task_by_pid(pid);
-		if (p) {
-			send_sigurg_to_task(p, fown);
-		}
-	} else {
-		do_each_task_pid(-pid, PIDTYPE_PGID, p) {
-			send_sigurg_to_task(p, fown);
-		} while_each_task_pid(-pid, PIDTYPE_PGID, p);
-	}
+	do_each_pid_task(pid, type, p) {
+		send_sigurg_to_task(p, fown);
+	} while_each_pid_task(pid, type, p);
 	read_unlock(&tasklist_lock);
  out_unlock_fown:
 	read_unlock(&fown->lock);
diff --git a/fs/file_table.c b/fs/file_table.c
index 2d9069c..c64764a 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -181,6 +181,7 @@ #endif
 	fops_put(file->f_op);
 	if (file->f_mode & FMODE_WRITE)
 		put_write_access(inode);
+	put_pid(file->f_owner.pid);
 	file_kill(file);
 	file->f_dentry = NULL;
 	file->f_vfsmnt = NULL;
diff --git a/fs/locks.c b/fs/locks.c
index d7c5339..b12e819 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -1514,7 +1514,7 @@ int fcntl_setlease(unsigned int fd, stru
 		goto out_unlock;
 	}
 
-	error = f_setown(filp, current->pid, 0);
+	error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
 out_unlock:
 	unlock_kernel();
 	return error;
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 50e6eb2..b74d39b 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -667,7 +667,8 @@ extern struct block_device *I_BDEV(struc
 
 struct fown_struct {
 	rwlock_t lock;          /* protects pid, uid, euid fields */
-	int pid;		/* pid or -pgrp where SIGIO should be sent */
+	struct pid *pid;	/* pid or -pgrp where SIGIO should be sent */
+	enum pid_type pid_type;	/* Kind of process group SIGIO should be sent to */
 	uid_t uid, euid;	/* uid/euid of process setting the owner */
 	int signum;		/* posix.1b rt signal to be delivered on IO */
 };
@@ -919,8 +920,10 @@ extern void kill_fasync(struct fasync_st
 /* only for net: no internal synchronization */
 extern void __kill_fasync(struct fasync_struct *, int, int);
 
+extern int __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
 extern int f_setown(struct file *filp, unsigned long arg, int force);
 extern void f_delown(struct file *filp);
+extern pid_t f_getown(struct file *filp);
 extern int send_sigurg(struct fown_struct *fown);
 
 /*
diff --git a/kernel/futex.c b/kernel/futex.c
index d4633c5..d78d898 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -1589,7 +1589,7 @@ static int futex_fd(u32 __user *uaddr, i
 	filp->f_mapping = filp->f_dentry->d_inode->i_mapping;
 
 	if (signal) {
-		err = f_setown(filp, current->pid, 1);
+		err = __f_setown(filp, task_pid(current), PIDTYPE_PID, 1);
 		if (err < 0) {
 			goto error;
 		}
diff --git a/net/socket.c b/net/socket.c
index 2bfc60a..a66bd4c 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -825,7 +825,7 @@ #endif				/* CONFIG_WIRELESS_EXT */
 			break;
 		case FIOGETOWN:
 		case SIOCGPGRP:
-			err = put_user(sock->file->f_owner.pid,
+			err = put_user(f_getown(sock->file),
 				       (int __user *)argp);
 			break;
 		case SIOCGIFBR:
-- 
1.4.2.rc3.g7e18e-dirty


  parent reply	other threads:[~2006-08-15 18:25 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-15 18:21 Start using struct pid Eric W. Biederman
2006-08-15 18:23 ` [PATCH 1/7] pid: Implement access helpers for a tacks various process groups Eric W. Biederman
2006-08-15 18:40   ` Dave Hansen
2006-08-15 19:03     ` Eric W. Biederman
2006-08-16  8:04     ` [Containers] " Kirill Korotaev
2006-08-15 18:23 ` [PATCH 2/7] pid: Add do_each_pid_task Eric W. Biederman
2006-08-16  3:10   ` [Containers] " Serge E. Hallyn
2006-08-16  4:28     ` Andrew Morton
2006-08-16  6:15       ` Eric W. Biederman
2006-08-16  6:34     ` Eric W. Biederman
2006-08-16 11:57       ` Serge E. Hallyn
2006-08-16 19:17   ` Oleg Nesterov
2006-08-17 21:16   ` [PATCH -mm] simplify pid iterators Oleg Nesterov
2006-08-15 18:23 ` [PATCH 3/7] pid: Implement signal functions that take a struct pid * Eric W. Biederman
2006-08-15 18:23 ` [PATCH 4/7] pid: Export the symbols needed to use " Eric W. Biederman
2006-08-15 18:23 ` [PATCH 5/7] pid: Implement pid_nr Eric W. Biederman
2006-08-15 18:37   ` Dave Hansen
2006-08-15 19:00     ` Eric W. Biederman
2006-08-15 19:15       ` [Containers] " Dave Hansen
2006-08-16  6:29         ` Eric W. Biederman
2006-08-16 16:27     ` Jan Engelhardt
2006-08-16 17:48       ` Eric W. Biederman
2006-08-16 18:19   ` Oleg Nesterov
2006-08-16 16:18     ` Eric W. Biederman
2006-08-16 21:03       ` Oleg Nesterov
2006-08-16 17:18         ` Eric W. Biederman
2006-08-15 18:23 ` [PATCH 6/7] vt: Update spawnpid to be a struct pid_t Eric W. Biederman
2006-08-15 18:53   ` Alan Cox
2006-08-15 18:45     ` Eric W. Biederman
2006-08-16  8:04       ` [Containers] " Kirill Korotaev
2006-08-16 14:23         ` Eric W. Biederman
2006-08-15 19:38   ` Ray Lehtiniemi
2006-08-16 19:35   ` Oleg Nesterov
2006-08-16 15:43     ` Andrew Morton
2006-08-16 17:55       ` Eric W. Biederman
2006-08-15 18:23 ` Eric W. Biederman [this message]
2006-08-16 18:45   ` [PATCH 7/7] file: Modify struct fown_struct to use a struct pid Oleg Nesterov
2006-08-16 18:48     ` Oleg Nesterov

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=11556661943487-git-send-email-ebiederm@xmission.com \
    --to=ebiederm@xmission.com \
    --cc=akpm@osdl.org \
    --cc=containers@lists.osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleg@tv-sign.ru \
    /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).