Skip to contentSkip to conversation

shift current_cred() from __f_setown() to f_modown()

PATCH In review Oleg Nesterov 4 participants 8 messages Jun 16, 2009 8 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw

PATCH 1/1 shift current_cred() from __f_setown() to f_modown()

Shift current_cred() from __f_setown() to f_modown(). This reduces the number of arguments and saves 48 bytes from fs/fcntl.o.

fs/fcntl.c renamed from fs/fcntl.c~f_modown +10 -7
@@ -196,15 +196,19 @@ static int setfl(int fd, struct file * f
 }
 
 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
-                     uid_t uid, uid_t euid, int force)
+                     int force)
 {
 	write_lock_irq(&filp->f_owner.lock);
 	if (force || !filp->f_owner.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;
+
+		if (pid) {
+			const struct cred *cred = current_cred();
+			filp->f_owner.uid = cred->uid;
+			filp->f_owner.euid = cred->euid;
+		}
 	}
 	write_unlock_irq(&filp->f_owner.lock);
 }
@@ -212,14 +216,13 @@ static void f_modown(struct file *filp, 
 int __f_setown(struct file *filp, struct pid *pid, enum pid_type type,
 		int force)
 {
-	const struct cred *cred = current_cred();
 	int err;
-	
+
 	err = security_file_set_fowner(filp);
 	if (err)
 		return err;
 
-	f_modown(filp, pid, type, cred->uid, cred->euid, force);
+	f_modown(filp, pid, type, force);
 	return 0;
 }
 EXPORT_SYMBOL(__f_setown);
@@ -245,7 +248,7 @@ EXPORT_SYMBOL(f_setown);
 
 void f_delown(struct file *filp)
 {
-	f_modown(filp, NULL, PIDTYPE_PID, 0, 0, 1);
+	f_modown(filp, NULL, PIDTYPE_PID, 1);
 }
 
 pid_t f_getown(struct file *filp)
#7 lore raw
In reply to Oleg Nesterov
Quoted 3 lines · Oleg Nesterov

Shift current_cred() from __f_setown() to f_modown(). This reduces the number of arguments and saves 48 bytes from fs/fcntl.o.

Ok, I know I asked for this, but now I suddenly start worrying about whether f_owner.uid/euid are initialized at all for the pid==NULL case?

They used to be initialized to zero, now they are left alone. Are they initialized somewhere else earlier?

Linus

#8 lore raw
Quoted 4 lines · Linus Torvalds
Quoted 1 line · Oleg Nesterov
Quoted 8 lines
Quoted 2 lines

Shift current_cred() from __f_setown() to f_modown(). This reduces the number of arguments and saves 48 bytes from fs/fcntl.o.

Ok, I know I asked for this, but now I suddenly start worrying about whether f_owner.uid/euid are initialized at all for the pid==NULL case?

They used to be initialized to zero, now they are left alone. Are they initialized somewhere else earlier?

I think this is fine, but I should have mentioned this in the changelog.

If f_owner.pid == NULL we never use f_owner.uid/euid. Otherwise we have a bug anyway: we must not send signals if pid was reset to NULL.

Oleg.

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#13 lore raw

PATCH 1/1 send_sigio_to_task: sanitize the usage of fown->signum

send_sigio_to_task() reads fown->signum several times, we can race with F_SETSIG which changes ->signum lockless. In theory, this can fool security checks or we can call group_send_sig_info() with the wrong ->si_signo which does not match "int sig".

Change the code to cache ->signum.

fs/fcntl.c renamed from fs/fcntl.c~signum +11 -5
@@ -426,14 +426,20 @@ static inline int sigio_perm(struct task
 }
 
 static void send_sigio_to_task(struct task_struct *p,
-			       struct fown_struct *fown, 
+			       struct fown_struct *fown,
 			       int fd,
 			       int reason)
 {
-	if (!sigio_perm(p, fown, fown->signum))
+	/*
+	 * F_SETSIG can change ->signum lockless in parallel, make
+	 * sure we read it once and use the same value throughout.
+	 */
+	int signum = ACCESS_ONCE(fown->signum);
+
+	if (!sigio_perm(p, fown, signum))
 		return;
 
-	switch (fown->signum) {
+	switch (signum) {
 		siginfo_t si;
 		default:
 			/* Queue a rt signal with the appropriate fd as its
@@ -442,7 +448,7 @@ static void send_sigio_to_task(struct ta
 			   delivered even if we can't queue.  Failure to
 			   queue in this case _should_ be reported; we fall
 			   back to SIGIO in that case. --sct */
-			si.si_signo = fown->signum;
+			si.si_signo = signum;
 			si.si_errno = 0;
 		        si.si_code  = reason;
 			/* Make sure we are called with one of the POLL_*
@@ -454,7 +460,7 @@ static void send_sigio_to_task(struct ta
 			else
 				si.si_band = band_table[reason - POLL_IN];
 			si.si_fd    = fd;
-			if (!group_send_sig_info(fown->signum, &si, p))
+			if (!group_send_sig_info(signum, &si, p))
 				break;
 		/* fall-through: fall back on the old plain SIGIO signal */
 		case 0:
Reviewed-by on v1 1/1
#14 lore raw

Re: [PATCH] send_sigio_to_task: sanitize the usage of fown->signum

Quoted 8 lines · Oleg Nesterov

send_sigio_to_task() reads fown->signum several times, we can race with F_SETSIG which changes ->signum lockless. In theory, this can fool security checks or we can call group_send_sig_info() with the wrong ->si_signo which does not match "int sig".

Change the code to cache ->signum.

Signature · 2 lines
James Morris
<[email protected]>
Acked-by on v1 1/1
#15 lore raw

Re: [PATCH] send_sigio_to_task: sanitize the usage of fown->signum

In reply to Oleg Nesterov
Quoted 8 lines · Oleg Nesterov

send_sigio_to_task() reads fown->signum several times, we can race with F_SETSIG which changes ->signum lockless. In theory, this can fool security checks or we can call group_send_sig_info() with the wrong ->si_signo which does not match "int sig".

Change the code to cache ->signum.