Skip to contentSkip to conversation

Improved getrusage

PATCH New Claudio Scordino 1 participant 1 message Apr 18, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Improved getrusage

Hi all!

A couple of months ago we discussed the possibility of improving the getrusage syscall to read usage information about tasks belonging to the same owner (not just about current and children).

After an extensive discussion on the mailing list, some of us agreed about the following patch. Some others, instead, said that it is too permissive.

Should we definitely drop this idea ??

Thanks,

Claudio

sys.new.c renamed from sys.c +23 -3
@@ -1767,9 +1767,29 @@
 
 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
 {
-	if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
-		return -EINVAL;
-	return getrusage(current, who, ru);
+	struct rusage r;
+	struct task_struct* tsk = current;
+	read_lock(&tasklist_lock);
+	if ((who != RUSAGE_SELF) && (who != RUSAGE_CHILDREN)) {
+		tsk = find_task_by_pid(who);
+		if ((tsk == NULL) || (who <=0)) 
+			goto bad;
+		if (((current->uid != tsk->euid) ||
+			(current->uid != tsk->suid) ||
+			(current->uid != tsk->uid) ||
+			(current->gid != tsk->egid) ||
+			(current->gid != tsk->sgid) ||
+			(current->gid != tsk->gid)) && !capable(CAP_SYS_PTRACE))
+				goto bad;
+		who = RUSAGE_SELF;
+	}
+	k_getrusage(tsk, who, &r);
+	read_unlock(&tasklist_lock);
+	return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
+
+bad:
+	read_unlock(&tasklist_lock);
+	return tsk ? -EPERM : -EINVAL;
 }
 
 asmlinkage long sys_umask(int mask)