Skip to contentSkip to conversation

System call to switch user credentials

RFC PATCH In review Jim Lieb 5 participants 22 messages Oct 16, 2013 – Nov 1, 2013

Subsystems (4)
lore
Share & tools
lore

Jim Lieb

author
#0 lore raw

cover System call to switch user credentials

This system call implementation is the result of discussions at LSF this last Feb about providing better kernel support to user mode file servers.

Our use case is an NFS+pNFS+9P file server in user space. We have to switch user credentials for a number of operations such as CREATE, MKDIR, and WRITE. We currently use setfsuid(), setfsgid(), and setgroups() for each of these calls followed by the same set of syscalls to revert to root privileges. We also must do a setcap to disable root privs so that quotas and access checks work properly. This results in a minimum of 7 system calls for each affected filesystem operation. Each syscall in this set creates a new creds object with its associated RCU resources.

Knfsd calls nfsd_setuser() to do the same thing in one call.

This system call does the same function as nfsd_setuser() but for user space. It replaces the six system calls with just two and uses RCU more efficiently by only doing it once. This is done using the following struct which combines all of the arguments that are passed but the corrent syscalls and passes them to the system call.

	struct user_creds {
		uid_t		uid;
		gid_t		gid;
		unsigned	ngroups;
		gid_t		altgroups[0];
	};

Inside our server, we have implemented two functions to manage credentials using a local user identity cache that has the following structure. The req_ctx contains two members of interest, a pointer to the credentials that are constructed by the server from the protocol and a file descriptor which is described below. The rest of the structure is housekeeping for the user identity cache.

	struct req_ctx {
		/* other stuff like avl tree links */
		struct user_creds *creds;
		int creds_fd;
	};

The typical sequence for a protocol operation that creates or morphs an object in the filesystem is:

	ctx = get_ctx(me->uid);
	become_client(ctx);
	/* mkdir, mknod, write as client user */
	restore_creds();

I have left out error handling to simplify the flow. This replaces the current setfsuid();setfsgid();setgroups(); before and after. get_ctx() does a lookup in the cache.

The become_client function uses two forms of the system call. We will take the second first. The SWCREDS_FSIDS command creates a new creds for the task and fills it from the user_creds argument. It also clears the set of root capabilities in the effective capabilities. This is functionally equivalent to what nfsd_setuser() does for knfsd.

We currently set the reduced capabilities globally in order to keep the overhead down. This system call does this per call, leaving the rest of the server with full capabilities.

This version of the system call opens an anonymous file and returns an fd for it. This fd is useless for I/O but it does allow us to cache creds cheaply. We close the file when we purge the cache entry.

The first form uses the SWCREDS_FROMFD command and the appropriate fd that was returned for this client user earlier. The creds referenced by the fd are used to override_creds the task's effective creds. Actually, any open file will do but the opened anonymous file is the least overhead because all it consumes is a filp and fd slot. The override_creds does not consume any RCU resources so it is much faster and consumes fewer resources.

int become_client(struct req_ctx *ctx)

{
	int ret;

	if (ctx->creds_fd >= 0) {
		ret = switch_creds(SWCREDS_FROMFD, ctx->creds_fd);
		if (ret < 0) {
			perror("become_client failed!");
			return ret;
		}
	} else {
		ret = switch_creds(SWCREDS_FSIDS, (unsigned long)ctx->creds);
		if (ret < 0) {
			perror("become_client with creds failed!");
			return ret;
		} else {
			fprintf(stderr, "New client: uid= %d, fd = %d\n",
			       ctx->creds->uid, ret);
			ctx->creds_fd = ret;
		}
	}
	return 0;
}

The restore_creds function simply uses the SWCREDS_REVERT command which restores the task's real creds. This is the safest route in our code but one could also switch directly to another set safely.

int restore_creds(void)

{
	int ret;

	ret = switch_creds(SWCREDS_REVERT, 0);
	if (ret < 0) {
		perror("switch_creds back failed!\n");
		return ret;
	}
	return 0;
}

The first patch implements the system call itself. The second two add the syscall linkage for X86 and X86_64. I chose the next available numbers for those architectures as of 3.12-RC5. I added these patches as a temporary bridge until official numbers are assigned. I have also not added entries for other architectures but there is nothing architecturally dependent in this syscall so when appropriate, numbers can be assigned.

Please review and comment to me. The code fragments above are from my test program.

Regards,

Jim Lieb
NFS Ganesha project

author
#1 lore raw

PATCH 1/3 switch_creds: Syscall to switch creds for file server ops

File servers must do some operations with the credentials of their client. This syscall switches the key credentials similar to nfsd_setuser() in fs/nfsd/auth.c with the capability of retaining a handle to the credentials by way of an fd for an open anonymous file. This makes switching for subsequent operations for that client more efficient.

---
 include/linux/cred.h     |  15 ++++
 include/linux/syscalls.h |   2 +
 kernel/sys.c             | 175 +++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sys_ni.c          |   3 +
 4 files changed, 195 insertions(+)
include/linux/cred.h +15 -0
index 04421e8..66a006e 100644
@@ -138,6 +138,21 @@ struct cred {
 	struct rcu_head	rcu;		/* RCU deletion hook */
 };
 
+/* switch_creds() syscall parameters*/
+
+#define SWCREDS_REVERT 0
+#define SWCREDS_FROMFD 1
+#define SWCREDS_FSIDS  2
+
+/* arg for SWCREDS_FSIDS command */
+
+struct user_creds {
+	uid_t		uid;
+	gid_t		gid;
+	unsigned	ngroups;
+	gid_t		altgroups[0];
+};
+
 extern void __put_cred(struct cred *);
 extern void exit_creds(struct task_struct *);
 extern int copy_creds(struct task_struct *, unsigned long);
include/linux/syscalls.h +2 -0
index 7fac04e..3550a8f 100644
@@ -64,6 +64,7 @@ struct old_linux_dirent;
 struct perf_event_attr;
 struct file_handle;
 struct sigaltstack;
+struct user_creds;
 
 #include <linux/types.h>
 #include <linux/aio_abi.h>
@@ -231,6 +232,7 @@ asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid);
 asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid);
 asmlinkage long sys_setfsuid(uid_t uid);
 asmlinkage long sys_setfsgid(gid_t gid);
+asmlinkage long sys_switch_creds(int cmd, unsigned long arg);
 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid);
 asmlinkage long sys_setsid(void);
 asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist);
kernel/sys.c +175 -0

187 lines not on this page · raw message

kernel/sys_ni.c +3 -0
index 7078052..7573cad 100644
@@ -209,3 +209,6 @@ cond_syscall(compat_sys_open_by_handle_at);
 
 /* compare kernel pointers */
 cond_syscall(sys_kcmp);
+
+/* switch task creds */
+cond_syscall(sys_switch_creds);
Signature · 1 line
1.8.3.1
maintainer
#2 lore raw

Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 5 lines · Jim Lieb

File servers must do some operations with the credentials of their client. This syscall switches the key credentials similar to nfsd_setuser() in fs/nfsd/auth.c with the capability of retaining a handle to the credentials by way of an fd for an open anonymous file. This makes switching for subsequent operations for that client more efficient.

Yet Another Untyped Multiplexor. Inna bun. Onna stick.
CMOT Dibbler special...

Switching creds to those of opener of given file descriptor
is fine, but in any realistic situation you'll get all the real win from that - you should cache those fds (which you seem to do), and then setuid/etc. is done once per cache miss. Making the magical "set them all at once" mess (complete with non-trivial structure, 32/64bit compat, etc.) pointless. Moreover, you don't need any magic files at all - just set the creds and open /dev/null and there's your fd. With proper creds associated with it. While we are at it, just _start_ with opening /dev/null. With your initial creds. Voila - revert is simply switch to that fd's creds.

IOW, you really need only one syscall:

SYSCALL_DEFINE1(switch_cred, int, fd)

{
	struct fd f = fdget(fd);
	if (!f.file)
		return -EBADF;
	put_cred(override_creds(f.file->f_cred);
	fdput(f);
	return 0;
}

and that's all there is to it.

#3 lore raw

Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 21 lines · Al Viro
Quoted 5 lines · Jim Lieb

File servers must do some operations with the credentials of their client. This syscall switches the key credentials similar to nfsd_setuser() in fs/nfsd/auth.c with the capability of retaining a handle to the credentials by way of an fd for an open anonymous file. This makes switching for subsequent operations for that client more efficient.

Yet Another Untyped Multiplexor. Inna bun. Onna stick.
CMOT Dibbler special...

Switching creds to those of opener of given file descriptor
is fine, but in any realistic situation you'll get all the real win from that - you should cache those fds (which you seem to do), and then setuid/etc. is done once per cache miss. Making the magical "set them all at once" mess (complete with non-trivial structure, 32/64bit compat, etc.) pointless. Moreover, you don't need any magic files at all - just set the creds and open /dev/null and there's your fd. With proper creds associated with it. While we are at it, just _start_ with opening /dev/null. With your initial creds. Voila - revert is simply switch to that fd's creds.

Quoted 1 line

IOW, you really need only one syscall:

That doesn't look bad but it does need capable(CAP_SETUID) && capable(CAP_SETGID) or possibly something a little more refined.

I don't think we want file descriptor passing to all of a sudden become a grant of privilege, beyond what the passed fd can do.

Quoted 11 lines

SYSCALL_DEFINE1(switch_cred, int, fd)

{
	struct fd f = fdget(fd);
	if (!f.file)
		return -EBADF;
	put_cred(override_creds(f.file->f_cred);
	fdput(f);
	return 0;
}

and that's all there is to it.

Eric

maintainer
#4 lore raw

Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 2 lines · Eric W. Biederman

That doesn't look bad but it does need capable(CAP_SETUID) && capable(CAP_SETGID) or possibly something a little more refined.

D'oh

Quoted 2 lines

I don't think we want file descriptor passing to all of a sudden become a grant of privilege, beyond what the passed fd can do.

Definitely. And an extra ) to make it compile wouldn't hurt either...

author
#5 lore raw

Re: Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 10 lines · On Thursday, October 17, 2013 02:20:50 Al Viro wrote:
Quoted 2 lines · Eric W. Biederman

That doesn't look bad but it does need capable(CAP_SETUID) && capable(CAP_SETGID) or possibly something a little more refined.

D'oh

Quoted 2 lines

I don't think we want file descriptor passing to all of a sudden become a grant of privilege, beyond what the passed fd can do.

Definitely. And an extra ) to make it compile wouldn't hurt either...

Ok, I'll rework this, dropping the void arg etc. How about this:

1. have one arg, the fd, i.e. SYSCALL_DEFINE1(switch_cred, int, fd)

2. if the fd >=0 do the override in my "use the fd" variation. This would do the capability check after the valid fd check. This means that you must have privs to mess with privs. Returns 0 or either EBADF or EPERM

3. if the fd == -1 do the revert case. The reason for this is there are 4 syscalls needed to change the creds and each has an error return. We need a way to escape the damage and a revert to the real creds set is the best way to return to a known state. This does not require a capability check because all that can happen is to return to the immutable real set. Also, I don't need the initial open of /dev/null.

Does this fit?

Jim

Signature · 10 lines
Jim Lieb
Linux Systems Engineer
Panasas Inc.

"If ease of use was the only requirement, we would all be riding tricycles"
- Douglas Engelbart 1925–2013
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#6 lore raw

Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

In reply to Al Viro
Quoted 11 lines · Al Viro
Quoted 2 lines · Eric W. Biederman

That doesn't look bad but it does need capable(CAP_SETUID) && capable(CAP_SETGID) or possibly something a little more refined.

D'oh

Quoted 2 lines

I don't think we want file descriptor passing to all of a sudden become a grant of privilege, beyond what the passed fd can do.

Definitely. And an extra ) to make it compile wouldn't hurt either...

There also appears to need to be a check that we don't gain any capabilities.

We also need a check so that you don't gain any capabilities, and possibly a few other things.

So I suspect we want a check something like:

if ((new_cred->securebits != current_cred->securebits) ||

    (new_cred->cap_inheritable != current_cred->cap_inheritable) ||
    (new_cred->cap_permitted != current_cred->cap_permitted) ||
    (new_cred->cap_effective != current_cred->cap_effective) ||
    (new_cred->cap_bset != current_cred->cap_bset) ||
    (new_cred->jit_keyring != current_cred->jit_keyring) ||
    (new_cred->session_keyring != current_cred->session_keyring) ||
    (new_cred->process_keyring != current_cred->process_keyring) ||
    (new_cred->thread_keyring != current_cred->thread_keyring) ||
    (new_cred->request_keyring != current_cred->request_keyring) ||
    (new_cred->security != current_cred->security) ||
    (new_cred->user_ns != current_cred->user_ns)) {
	return -EPERM;
}

Eric

#7 lore raw

Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 12 lines · Eric W. Biederman
Quoted 10 lines · Al Viro
Quoted 2 lines · Eric W. Biederman

That doesn't look bad but it does need capable(CAP_SETUID) && capable(CAP_SETGID) or possibly something a little more refined.

D'oh

Quoted 2 lines

I don't think we want file descriptor passing to all of a sudden become a grant of privilege, beyond what the passed fd can do.

Quoted 7 lines
Quoted 1 line

Definitely. And an extra ) to make it compile wouldn't hurt either...

There also appears to need to be a check that we don't gain any capabilities.

We also need a check so that you don't gain any capabilities, and possibly a few other things.

Why? I like the user_ns part, but I'm not immediately seeing the issue with capabilities.

Quoted 4 lines

So I suspect we want a check something like:

if ((new_cred->securebits != current_cred->securebits) ||

Quoted 14 lines
    (new_cred->cap_inheritable != current_cred->cap_inheritable) ||
    (new_cred->cap_permitted != current_cred->cap_permitted) ||
    (new_cred->cap_effective != current_cred->cap_effective) ||
    (new_cred->cap_bset != current_cred->cap_bset) ||
    (new_cred->jit_keyring != current_cred->jit_keyring) ||
    (new_cred->session_keyring != current_cred->session_keyring) ||
    (new_cred->process_keyring != current_cred->process_keyring) ||
    (new_cred->thread_keyring != current_cred->thread_keyring) ||
    (new_cred->request_keyring != current_cred->request_keyring) ||
    (new_cred->security != current_cred->security) ||
    (new_cred->user_ns != current_cred->user_ns)) {
	return -EPERM;
}

I *really* don't like the idea of being able to use any old file descriptor. I barely care what rights the caller needs to have to invoke this -- if you're going to pass an fd that grants a capability (in the non-Linux sense of the work), please make sure that the sender actually wants that behavior.

IOW, have a syscall to generate a special fd for this purpose. It's only a couple lines of code, and I think we'll really regret it if we fsck this up.

(I will take it as a personal challenge to find at least one exploitable privilege escalation in this if an arbitrary fd works.)

Also... real_cred looks confusing. AFAICS it is used *only* for knfsd and faccessat. That is, current userspace can't see it. But now you'll expose various oddities. For example, AFAICS a capability-less process that's a userns owner can always use setuid. This will *overwrite* real_cred. Then you're screwed, especially if this happens by accident.

That being said, Windows has had functions like this for a long time. Processes have a primary token and possibly an impersonation token. Any process can call ImpersonateLoggedOnUser (no privilege required) to impersonate the credentials of a token (which is special kind of fd). Similarly, any process can call RevertToSelf to undo it.

Is there any actual problem with allowing completely unprivileged tasks to switch to one of these magic cred fds? That would avoid needing a "revert" operation.

Another note: I think that there may be issues if the creator of a token has no_new_privs set and the user doesn't. Imagine a daemon that accepts one of these fds, impersonates it, and calls exec. This could be used to escape from no_new_privs land.

--Andy

#8 lore raw

Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 15 lines · Andy Lutomirski
Quoted 14 lines · Eric W. Biederman
Quoted 11 lines · Al Viro
Quoted 2 lines · Eric W. Biederman

That doesn't look bad but it does need capable(CAP_SETUID) && capable(CAP_SETGID) or possibly something a little more refined.

D'oh

Quoted 2 lines

I don't think we want file descriptor passing to all of a sudden become a grant of privilege, beyond what the passed fd can do.

Definitely. And an extra ) to make it compile wouldn't hurt either...

Quoted 8 lines
Quoted 5 lines

There also appears to need to be a check that we don't gain any capabilities.

We also need a check so that you don't gain any capabilities, and possibly a few other things.

Why? I like the user_ns part, but I'm not immediately seeing the issue with capabilities.

My reasoning was instead of making this syscall as generic as possible start it out by only allowing the cases Jim cares about and working with a model where you can't gain any permissions you couldn't gain otherwise.

Although the fd -1 trick to revert to your other existing cred seems reasonable.

Quoted 24 lines
Quoted 17 lines

So I suspect we want a check something like:

if ((new_cred->securebits != current_cred->securebits) ||

    (new_cred->cap_inheritable != current_cred->cap_inheritable) ||
    (new_cred->cap_permitted != current_cred->cap_permitted) ||
    (new_cred->cap_effective != current_cred->cap_effective) ||
    (new_cred->cap_bset != current_cred->cap_bset) ||
    (new_cred->jit_keyring != current_cred->jit_keyring) ||
    (new_cred->session_keyring != current_cred->session_keyring) ||
    (new_cred->process_keyring != current_cred->process_keyring) ||
    (new_cred->thread_keyring != current_cred->thread_keyring) ||
    (new_cred->request_keyring != current_cred->request_keyring) ||
    (new_cred->security != current_cred->security) ||
    (new_cred->user_ns != current_cred->user_ns)) {
	return -EPERM;
}

I *really* don't like the idea of being able to use any old file descriptor. I barely care what rights the caller needs to have to invoke this -- if you're going to pass an fd that grants a capability (in the non-Linux sense of the work), please make sure that the sender actually wants that behavior.

Quoted 6 lines

IOW, have a syscall to generate a special fd for this purpose. It's only a couple lines of code, and I think we'll really regret it if we fsck this up.

(I will take it as a personal challenge to find at least one exploitable privilege escalation in this if an arbitrary fd works.)

If you can't switch to a uid or a gid you couldn't switch to otherwise then the worst that can happen is an information leak. And information leaks are rarely directly exploitable.

Quoted 6 lines

Also... real_cred looks confusing. AFAICS it is used *only* for knfsd and faccessat. That is, current userspace can't see it. But now you'll expose various oddities. For example, AFAICS a capability-less process that's a userns owner can always use setuid. This will *overwrite* real_cred. Then you're screwed, especially if this happens by accident.

And doing in userland what faccessat, and knfsd do in the kernel is exactly what is desired here. But maybe there are issues with that.

Quoted 9 lines

That being said, Windows has had functions like this for a long time. Processes have a primary token and possibly an impersonation token. Any process can call ImpersonateLoggedOnUser (no privilege required) to impersonate the credentials of a token (which is special kind of fd). Similarly, any process can call RevertToSelf to undo it.

Is there any actual problem with allowing completely unprivileged tasks to switch to one of these magic cred fds? That would avoid needing a "revert" operation.

If the permission model is this switching of credentials doesn't get you anything you couldn't get some other way. That would seem to totally rules out unprivileged processes switching these things.

Quoted 4 lines

Another note: I think that there may be issues if the creator of a token has no_new_privs set and the user doesn't. Imagine a daemon that accepts one of these fds, impersonates it, and calls exec. This could be used to escape from no_new_privs land.

Which is why I was suggesting that we don't allow changing any field in the cred except for uids and gids.

Eric

#9 lore raw

Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 62 lines · Eric W. Biederman
Quoted 15 lines · Andy Lutomirski
Quoted 14 lines · Eric W. Biederman
Quoted 11 lines · Al Viro
Quoted 2 lines · Eric W. Biederman

That doesn't look bad but it does need capable(CAP_SETUID) && capable(CAP_SETGID) or possibly something a little more refined.

D'oh

Quoted 2 lines

I don't think we want file descriptor passing to all of a sudden become a grant of privilege, beyond what the passed fd can do.

Definitely. And an extra ) to make it compile wouldn't hurt either...

Quoted 8 lines
Quoted 5 lines

There also appears to need to be a check that we don't gain any capabilities.

We also need a check so that you don't gain any capabilities, and possibly a few other things.

Why? I like the user_ns part, but I'm not immediately seeing the issue with capabilities.

My reasoning was instead of making this syscall as generic as possible start it out by only allowing the cases Jim cares about and working with a model where you can't gain any permissions you couldn't gain otherwise.

Although the fd -1 trick to revert to your other existing cred seems reasonable.

Quoted 28 lines
Quoted 17 lines

So I suspect we want a check something like:

if ((new_cred->securebits != current_cred->securebits) ||

    (new_cred->cap_inheritable != current_cred->cap_inheritable) ||
    (new_cred->cap_permitted != current_cred->cap_permitted) ||
    (new_cred->cap_effective != current_cred->cap_effective) ||
    (new_cred->cap_bset != current_cred->cap_bset) ||
    (new_cred->jit_keyring != current_cred->jit_keyring) ||
    (new_cred->session_keyring != current_cred->session_keyring) ||
    (new_cred->process_keyring != current_cred->process_keyring) ||
    (new_cred->thread_keyring != current_cred->thread_keyring) ||
    (new_cred->request_keyring != current_cred->request_keyring) ||
    (new_cred->security != current_cred->security) ||
    (new_cred->user_ns != current_cred->user_ns)) {
     return -EPERM;
}

I *really* don't like the idea of being able to use any old file descriptor. I barely care what rights the caller needs to have to invoke this -- if you're going to pass an fd that grants a capability (in the non-Linux sense of the work), please make sure that the sender actually wants that behavior.

IOW, have a syscall to generate a special fd for this purpose. It's only a couple lines of code, and I think we'll really regret it if we fsck this up.

Quoted 6 lines
Quoted 2 lines

(I will take it as a personal challenge to find at least one exploitable privilege escalation in this if an arbitrary fd works.)

If you can't switch to a uid or a gid you couldn't switch to otherwise then the worst that can happen is an information leak. And information leaks are rarely directly exploitable.

Here's the attack:

Suppose there's a daemon that uses this in conjunction with SCM_RIGHTS. The daemon is effectively root (under the current proposal, it has to be). So a client connects, sends a credential fd, and the daemon impersonates those credentials.

Now a malicious user obtains *any* fd opened by root. It sends that fd to the daemon. The daemon then impersonates root. We lose. (It can't possibly be hard to obtain an fd with highly privileged f_cred -- I bet that most console programs have stdin like that, for example.
There are probably many setuid programs that will happily open
/dev/null for you, too.)

Quoted 17 lines
Quoted 7 lines

Also... real_cred looks confusing. AFAICS it is used *only* for knfsd and faccessat. That is, current userspace can't see it. But now you'll expose various oddities. For example, AFAICS a capability-less process that's a userns owner can always use setuid. This will *overwrite* real_cred. Then you're screwed, especially if this happens by accident.

And doing in userland what faccessat, and knfsd do in the kernel is exactly what is desired here. But maybe there are issues with that.

Quoted 6 lines

That being said, Windows has had functions like this for a long time. Processes have a primary token and possibly an impersonation token. Any process can call ImpersonateLoggedOnUser (no privilege required) to impersonate the credentials of a token (which is special kind of fd). Similarly, any process can call RevertToSelf to undo it.

Quoted 7 lines
Quoted 3 lines

Is there any actual problem with allowing completely unprivileged tasks to switch to one of these magic cred fds? That would avoid needing a "revert" operation.

If the permission model is this switching of credentials doesn't get you anything you couldn't get some other way. That would seem to totally rules out unprivileged processes switching these things.

IMO, there are two reasonable models that involve fds carrying some kind of credential.

1. The fd actually carries the ability to use the credentials. You need to be very careful whom you send these to. The security implications are obvious (which is good) and the receiver doesn't need privilege (which is good, as long as the receiver is careful).

2. The fd is for identification only. But this means that the fd carries the ability to identify as a user. So you *still* have to be very careful about whom you send it to. What you need is an operation that allows you to identify using the fd without transitively granting the recipient the same ability. On networks, this is done by signing some kind of challenge. The kernel could work the same way, or there could be a new CMSG_IDENTITY that you need an identity fd to send but that does not copy that fd to the recipient.

Quoted 8 lines
Quoted 5 lines

Another note: I think that there may be issues if the creator of a token has no_new_privs set and the user doesn't. Imagine a daemon that accepts one of these fds, impersonates it, and calls exec. This could be used to escape from no_new_privs land.

Which is why I was suggesting that we don't allow changing any field in the cred except for uids and gids.

If the daemon impersonates and execs, we still lose.

--Andy

author
#10 lore raw

Re: Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 79 lines · On Thursday, October 24, 2013 12:28:15 Andy Lutomirski wrote:

On Wed, Oct 23, 2013 at 10:59 PM, Eric W. Biederman

Quoted 62 lines · <[email protected]> wrote:
Quoted 14 lines · Andy Lutomirski
Quoted 13 lines · Eric W. Biederman
Quoted 11 lines · Al Viro
Quoted 2 lines · Eric W. Biederman

That doesn't look bad but it does need capable(CAP_SETUID) && capable(CAP_SETGID) or possibly something a little more refined.

D'oh

Quoted 3 lines

I don't think we want file descriptor passing to all of a sudden become
a grant of privilege, beyond what the passed fd can do.

Definitely. And an extra ) to make it compile wouldn't hurt either...

Quoted 8 lines
Quoted 5 lines

There also appears to need to be a check that we don't gain any capabilities.

We also need a check so that you don't gain any capabilities, and possibly a few other things.

Why? I like the user_ns part, but I'm not immediately seeing the issue with capabilities.

My reasoning was instead of making this syscall as generic as possible start it out by only allowing the cases Jim cares about and working with a model where you can't gain any permissions you couldn't gain otherwise.

Although the fd -1 trick to revert to your other existing cred seems reasonable.

Quoted 30 lines
Quoted 18 lines

So I suspect we want a check something like:

if ((new_cred->securebits != current_cred->securebits) ||

    (new_cred->cap_inheritable != current_cred->cap_inheritable) ||
    (new_cred->cap_permitted != current_cred->cap_permitted) ||
    (new_cred->cap_effective != current_cred->cap_effective) ||
    (new_cred->cap_bset != current_cred->cap_bset) ||
    (new_cred->jit_keyring != current_cred->jit_keyring) ||
    (new_cred->session_keyring != current_cred->session_keyring) ||
    (new_cred->process_keyring != current_cred->process_keyring) ||
    (new_cred->thread_keyring != current_cred->thread_keyring) ||
    (new_cred->request_keyring != current_cred->request_keyring) ||
    (new_cred->security != current_cred->security) ||
    (new_cred->user_ns != current_cred->user_ns)) {
    
     return -EPERM;
Quoted 1 line

}

I *really* don't like the idea of being able to use any old file descriptor. I barely care what rights the caller needs to have to invoke this -- if you're going to pass an fd that grants a capability (in the non-Linux sense of the work), please make sure that the sender actually wants that behavior.

IOW, have a syscall to generate a special fd for this purpose. It's only a couple lines of code, and I think we'll really regret it if we fsck this up.

Quoted 6 lines
Quoted 2 lines

(I will take it as a personal challenge to find at least one exploitable privilege escalation in this if an arbitrary fd works.)

If you can't switch to a uid or a gid you couldn't switch to otherwise then the worst that can happen is an information leak. And information leaks are rarely directly exploitable.

Here's the attack:

Suppose there's a daemon that uses this in conjunction with SCM_RIGHTS. The daemon is effectively root (under the current proposal, it has to be). So a client connects, sends a credential fd, and the daemon impersonates those credentials.

Quoted 6 lines

Now a malicious user obtains *any* fd opened by root. It sends that fd to the daemon. The daemon then impersonates root. We lose. (It can't possibly be hard to obtain an fd with highly privileged f_cred -- I bet that most console programs have stdin like that, for example.
There are probably many setuid programs that will happily open
/dev/null for you, too.)

In my reply to Eric, I note that I need to add a check that the fd passed is one from switch_creds. With that test, not any fd will do and the one that does has only been able to set fsuid, fsgid, altgroups, and reduced (the nfsd set) caps. They can do no more damage than what the original switch_creds allowed. The any fd by root no longer applies so use doesn't get much (no escalation).

Quoted 25 lines
Quoted 17 lines
Quoted 7 lines

Also... real_cred looks confusing. AFAICS it is used *only* for knfsd and faccessat. That is, current userspace can't see it. But now you'll expose various oddities. For example, AFAICS a capability-less process that's a userns owner can always use setuid. This will *overwrite* real_cred. Then you're screwed, especially if this happens by accident.

And doing in userland what faccessat, and knfsd do in the kernel is exactly what is desired here. But maybe there are issues with that.

Quoted 6 lines

That being said, Windows has had functions like this for a long time. Processes have a primary token and possibly an impersonation token. Any process can call ImpersonateLoggedOnUser (no privilege required) to impersonate the credentials of a token (which is special kind of fd). Similarly, any process can call RevertToSelf to undo it.

Quoted 7 lines
Quoted 3 lines

Is there any actual problem with allowing completely unprivileged tasks to switch to one of these magic cred fds? That would avoid needing a "revert" operation.

If the permission model is this switching of credentials doesn't get you anything you couldn't get some other way. That would seem to totally rules out unprivileged processes switching these things.

Quoted 7 lines

IMO, there are two reasonable models that involve fds carrying some kind of credential.

1. The fd actually carries the ability to use the credentials. You need to be very careful whom you send these to. The security implications are obvious (which is good) and the receiver doesn't need privilege (which is good, as long as the receiver is careful).

I test for caps. I think using switch_creds in all forms should require privs. I thought of the revert case and although it does simply return to "real" creds, you still need CAP_SETUID and CAP_SETGID to do it. This means, of course, if you have really messed up things, you may not be able to get back home which, although a bad thing for the process, is a good thing for the system as a whole.

Quoted 9 lines

2. The fd is for identification only. But this means that the fd carries the ability to identify as a user. So you *still* have to be very careful about whom you send it to. What you need is an operation that allows you to identify using the fd without transitively granting the recipient the same ability. On networks, this is done by signing some kind of challenge. The kernel could work the same way, or there could be a new CMSG_IDENTITY that you need an identity fd to send but that does not copy that fd to the recipient.

I am not sure I understand this. CMSG only applies to UNIX_DOMAIN sockets which means that the switch_creds fd test still applies here. It is identification but only for within the same kernel. As for namespaces, the translation was done when the creds fd was created. I suppose if it was passed across namespace boundaries there could be a problem but what is in the creds structure is the translated fduid,fsgid etc., not the untranslated one. We are still doing access checks and quota stuff with the translated creds. If one namespace has "fred" as uid 52 and another has "mary" as 52, the quota will only be credited to whoever "fred" really is only if "fred" gets to be "mary" in the second alternate universe...

Quoted 10 lines
Quoted 8 lines
Quoted 5 lines

Another note: I think that there may be issues if the creator of a token has no_new_privs set and the user doesn't. Imagine a daemon that accepts one of these fds, impersonates it, and calls exec. This could be used to escape from no_new_privs land.

Which is why I was suggesting that we don't allow changing any field in the cred except for uids and gids.

If the daemon impersonates and execs, we still lose.

I answered this. You only get to impersonate for purposes of file access with reduced caps to prevent you from being root as well. Also, since these are O_CLOEXEC and switch_creds "magic" fds, this can't happen because the fd is gone post-exec.

Quoted 3 lines

--Andy

Signature · 10 lines
Jim Lieb
Linux Systems Engineer
Panasas Inc.

"If ease of use was the only requirement, we would all be riding tricycles"
- Douglas Engelbart 1925–2013
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#11 lore raw

Re: Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 144 lines · Jim Lieb
Quoted 79 lines · On Thursday, October 24, 2013 12:28:15 Andy Lutomirski wrote:

On Wed, Oct 23, 2013 at 10:59 PM, Eric W. Biederman

Quoted 62 lines · <[email protected]> wrote:
Quoted 14 lines · Andy Lutomirski
Quoted 13 lines · Eric W. Biederman
Quoted 11 lines · Al Viro
Quoted 2 lines · Eric W. Biederman

That doesn't look bad but it does need capable(CAP_SETUID) && capable(CAP_SETGID) or possibly something a little more refined.

D'oh

Quoted 3 lines

I don't think we want file descriptor passing to all of a sudden become
a grant of privilege, beyond what the passed fd can do.

Definitely. And an extra ) to make it compile wouldn't hurt either...

Quoted 8 lines
Quoted 5 lines

There also appears to need to be a check that we don't gain any capabilities.

We also need a check so that you don't gain any capabilities, and possibly a few other things.

Why? I like the user_ns part, but I'm not immediately seeing the issue with capabilities.

My reasoning was instead of making this syscall as generic as possible start it out by only allowing the cases Jim cares about and working with a model where you can't gain any permissions you couldn't gain otherwise.

Although the fd -1 trick to revert to your other existing cred seems reasonable.

Quoted 30 lines
Quoted 18 lines

So I suspect we want a check something like:

if ((new_cred->securebits != current_cred->securebits) ||

    (new_cred->cap_inheritable != current_cred->cap_inheritable) ||
    (new_cred->cap_permitted != current_cred->cap_permitted) ||
    (new_cred->cap_effective != current_cred->cap_effective) ||
    (new_cred->cap_bset != current_cred->cap_bset) ||
    (new_cred->jit_keyring != current_cred->jit_keyring) ||
    (new_cred->session_keyring != current_cred->session_keyring) ||
    (new_cred->process_keyring != current_cred->process_keyring) ||
    (new_cred->thread_keyring != current_cred->thread_keyring) ||
    (new_cred->request_keyring != current_cred->request_keyring) ||
    (new_cred->security != current_cred->security) ||
    (new_cred->user_ns != current_cred->user_ns)) {

     return -EPERM;
Quoted 1 line

}

I *really* don't like the idea of being able to use any old file descriptor. I barely care what rights the caller needs to have to invoke this -- if you're going to pass an fd that grants a capability (in the non-Linux sense of the work), please make sure that the sender actually wants that behavior.

IOW, have a syscall to generate a special fd for this purpose. It's only a couple lines of code, and I think we'll really regret it if we fsck this up.

Quoted 6 lines
Quoted 2 lines

(I will take it as a personal challenge to find at least one exploitable privilege escalation in this if an arbitrary fd works.)

If you can't switch to a uid or a gid you couldn't switch to otherwise then the worst that can happen is an information leak. And information leaks are rarely directly exploitable.

Here's the attack:

Suppose there's a daemon that uses this in conjunction with SCM_RIGHTS. The daemon is effectively root (under the current proposal, it has to be). So a client connects, sends a credential fd, and the daemon impersonates those credentials.

Quoted 6 lines

Now a malicious user obtains *any* fd opened by root. It sends that fd to the daemon. The daemon then impersonates root. We lose. (It can't possibly be hard to obtain an fd with highly privileged f_cred -- I bet that most console programs have stdin like that, for example.
There are probably many setuid programs that will happily open
/dev/null for you, too.)

In my reply to Eric, I note that I need to add a check that the fd passed is one from switch_creds. With that test, not any fd will do and the one that does has only been able to set fsuid, fsgid, altgroups, and reduced (the nfsd set) caps. They can do no more damage than what the original switch_creds allowed. The any fd by root no longer applies so use doesn't get much (no escalation).

Quoted 25 lines
Quoted 17 lines
Quoted 7 lines

Also... real_cred looks confusing. AFAICS it is used *only* for knfsd and faccessat. That is, current userspace can't see it. But now you'll expose various oddities. For example, AFAICS a capability-less process that's a userns owner can always use setuid. This will *overwrite* real_cred. Then you're screwed, especially if this happens by accident.

And doing in userland what faccessat, and knfsd do in the kernel is exactly what is desired here. But maybe there are issues with that.

Quoted 6 lines

That being said, Windows has had functions like this for a long time. Processes have a primary token and possibly an impersonation token. Any process can call ImpersonateLoggedOnUser (no privilege required) to impersonate the credentials of a token (which is special kind of fd). Similarly, any process can call RevertToSelf to undo it.

Quoted 7 lines
Quoted 3 lines

Is there any actual problem with allowing completely unprivileged tasks to switch to one of these magic cred fds? That would avoid needing a "revert" operation.

If the permission model is this switching of credentials doesn't get you anything you couldn't get some other way. That would seem to totally rules out unprivileged processes switching these things.

Quoted 7 lines

IMO, there are two reasonable models that involve fds carrying some kind of credential.

1. The fd actually carries the ability to use the credentials. You need to be very careful whom you send these to. The security implications are obvious (which is good) and the receiver doesn't need privilege (which is good, as long as the receiver is careful).

I test for caps. I think using switch_creds in all forms should require privs. I thought of the revert case and although it does simply return to "real" creds, you still need CAP_SETUID and CAP_SETGID to do it. This means, of course, if you have really messed up things, you may not be able to get back home which, although a bad thing for the process, is a good thing for the system as a whole.

Quoted 9 lines

2. The fd is for identification only. But this means that the fd carries the ability to identify as a user. So you *still* have to be very careful about whom you send it to. What you need is an operation that allows you to identify using the fd without transitively granting the recipient the same ability. On networks, this is done by signing some kind of challenge. The kernel could work the same way, or there could be a new CMSG_IDENTITY that you need an identity fd to send but that does not copy that fd to the recipient.

Quoted 10 lines

I am not sure I understand this. CMSG only applies to UNIX_DOMAIN sockets which means that the switch_creds fd test still applies here. It is identification but only for within the same kernel. As for namespaces, the translation was done when the creds fd was created. I suppose if it was passed across namespace boundaries there could be a problem but what is in the creds structure is the translated fduid,fsgid etc., not the untranslated one. We are still doing access checks and quota stuff with the translated creds. If one namespace has "fred" as uid 52 and another has "mary" as 52, the quota will only be credited to whoever "fred" really is only if "fred" gets to be "mary" in the second alternate universe...

I'm not talking about namespaces here -- I think we're not really on the same page. Can you describe what you're envisioning doing with these fds?

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines

Another note: I think that there may be issues if the creator of a token has no_new_privs set and the user doesn't. Imagine a daemon that accepts one of these fds, impersonates it, and calls exec. This could be used to escape from no_new_privs land.

Which is why I was suggesting that we don't allow changing any field in the cred except for uids and gids.

Quoted 6 lines
Quoted 1 line

If the daemon impersonates and execs, we still lose.

I answered this. You only get to impersonate for purposes of file access with reduced caps to prevent you from being root as well. Also, since these are O_CLOEXEC and switch_creds "magic" fds, this can't happen because the fd is gone post-exec.

If a no_new_privs process authenticates to a daemon and that daemon execs, then there's now a dumpable, ptraceable, non-no-new-privs process running as the uid/gid of the no_new_privs process. This may be dangerous.

Quoted 12 lines
Quoted 4 lines

--Andy

--
Jim Lieb
Linux Systems Engineer
Panasas Inc.

"If ease of use was the only requirement, we would all be riding tricycles"
- Douglas Engelbart 1925–2013

Signature · 6 lines
Andy Lutomirski
AMA Capital Management, LLC
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author
#12 lore raw

Re: Re: Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 167 lines · On Thursday, October 31, 2013 12:09:08 Andy Lutomirski wrote:
Quoted 153 lines · Jim Lieb
Quoted 83 lines · On Thursday, October 24, 2013 12:28:15 Andy Lutomirski wrote:

On Wed, Oct 23, 2013 at 10:59 PM, Eric W. Biederman

Quoted 65 lines · <[email protected]> wrote:
Quoted 18 lines · Andy Lutomirski
Quoted 17 lines · Eric W. Biederman
Quoted 4 lines · Al Viro
Quoted 2 lines · Eric W. Biederman

That doesn't look bad but it does need capable(CAP_SETUID) && capable(CAP_SETGID) or possibly something a little more refined.

Quoted 8 lines

D'oh

Quoted 3 lines

I don't think we want file descriptor passing to all of a sudden become
a grant of privilege, beyond what the passed fd can do.

Definitely. And an extra ) to make it compile wouldn't hurt either...

There also appears to need to be a check that we don't gain any capabilities.

Quoted 6 lines
Quoted 2 lines

We also need a check so that you don't gain any capabilities, and possibly a few other things.

Why? I like the user_ns part, but I'm not immediately seeing the issue
with capabilities.

My reasoning was instead of making this syscall as generic as possible start it out by only allowing the cases Jim cares about and working with
a model where you can't gain any permissions you couldn't gain otherwise.

Although the fd -1 trick to revert to your other existing cred seems reasonable.

Quoted 30 lines
Quoted 18 lines

So I suspect we want a check something like:

if ((new_cred->securebits != current_cred->securebits) ||

    (new_cred->cap_inheritable != current_cred->cap_inheritable) ||
    (new_cred->cap_permitted != current_cred->cap_permitted) ||
    (new_cred->cap_effective != current_cred->cap_effective) ||
    (new_cred->cap_bset != current_cred->cap_bset) ||
    (new_cred->jit_keyring != current_cred->jit_keyring) ||
    (new_cred->session_keyring != current_cred->session_keyring) ||
    (new_cred->process_keyring != current_cred->process_keyring) ||
    (new_cred->thread_keyring != current_cred->thread_keyring) ||
    (new_cred->request_keyring != current_cred->request_keyring) ||
    (new_cred->security != current_cred->security) ||
    (new_cred->user_ns != current_cred->user_ns)) {
    
     return -EPERM;
Quoted 1 line

}

I *really* don't like the idea of being able to use any old file descriptor. I barely care what rights the caller needs to have to invoke this -- if you're going to pass an fd that grants a capability (in the non-Linux sense of the work), please make sure that the sender actually wants that behavior.

IOW, have a syscall to generate a special fd for this purpose. It's only a couple lines of code, and I think we'll really regret it if we fsck this up.

Quoted 7 lines
Quoted 3 lines

(I will take it as a personal challenge to find at least one exploitable
privilege escalation in this if an arbitrary fd works.)

If you can't switch to a uid or a gid you couldn't switch to otherwise then the worst that can happen is an information leak. And information leaks are rarely directly exploitable.

Here's the attack:

Suppose there's a daemon that uses this in conjunction with SCM_RIGHTS. The daemon is effectively root (under the current proposal, it has to be). So a client connects, sends a credential fd, and the daemon impersonates those credentials.

Quoted 8 lines

Now a malicious user obtains *any* fd opened by root. It sends that fd to the daemon. The daemon then impersonates root. We lose. (It can't possibly be hard to obtain an fd with highly privileged f_cred -- I bet that most console programs have stdin like that, for example.

There are probably many setuid programs that will happily open

/dev/null for you, too.)

In my reply to Eric, I note that I need to add a check that the fd passed is one from switch_creds. With that test, not any fd will do and the one that does has only been able to set fsuid, fsgid, altgroups, and reduced (the nfsd set) caps. They can do no more damage than what the original switch_creds allowed. The any fd by root no longer applies so use doesn't get much (no escalation).

Quoted 29 lines
Quoted 24 lines
Quoted 8 lines

Also... real_cred looks confusing. AFAICS it is used *only* for knfsd and faccessat. That is, current userspace can't see it. But now you'll
expose various oddities. For example, AFAICS a capability-less process
that's a userns owner can always use setuid. This will *overwrite* real_cred. Then you're screwed, especially if this happens by accident.

And doing in userland what faccessat, and knfsd do in the kernel is exactly what is desired here. But maybe there are issues with that.

Quoted 11 lines

That being said, Windows has had functions like this for a long time. Processes have a primary token and possibly an impersonation token. Any
process can call ImpersonateLoggedOnUser (no privilege required) to impersonate the credentials of a token (which is special kind of fd). Similarly, any process can call RevertToSelf to undo it.

Is there any actual problem with allowing completely unprivileged tasks
to switch to one of these magic cred fds? That would avoid needing a "revert" operation.

Quoted 4 lines

If the permission model is this switching of credentials doesn't get you
anything you couldn't get some other way. That would seem to totally rules out unprivileged processes switching these things.

Quoted 7 lines

IMO, there are two reasonable models that involve fds carrying some kind of credential.

1. The fd actually carries the ability to use the credentials. You need to be very careful whom you send these to. The security implications are obvious (which is good) and the receiver doesn't need privilege (which is good, as long as the receiver is careful).

I test for caps. I think using switch_creds in all forms should require privs. I thought of the revert case and although it does simply return to "real" creds, you still need CAP_SETUID and CAP_SETGID to do it. This means, of course, if you have really messed up things, you may not be able to get back home which, although a bad thing for the process, is a good thing for the system as a whole.

Quoted 8 lines

2. The fd is for identification only. But this means that the fd carries the ability to identify as a user. So you *still* have to be very careful about whom you send it to. What you need is an operation that allows you to identify using the fd without transitively granting the recipient the same ability. On networks, this is done by signing some kind of challenge. The kernel could work the same way, or there could be a new CMSG_IDENTITY that you need an identity fd to send but that does not copy that fd to the recipient.

Quoted 12 lines

I am not sure I understand this. CMSG only applies to UNIX_DOMAIN sockets which means that the switch_creds fd test still applies here. It is identification but only for within the same kernel. As for namespaces, the
translation was done when the creds fd was created. I suppose if it was passed across namespace boundaries there could be a problem but what is in the creds structure is the translated fduid,fsgid etc., not the untranslated one. We are still doing access checks and quota stuff with the translated creds. If one namespace has "fred" as uid 52 and another has "mary" as 52, the quota will only be credited to whoever "fred" really is only if "fred" gets to be "mary" in the second alternate universe...

Quoted 3 lines

I'm not talking about namespaces here -- I think we're not really on the same page. Can you describe what you're envisioning doing with these fds?

I have a new version that is now two syscalls and no multiplexing has more testing etc. to mirror exactly the tests in setfsuid(). I still am testing but plan to send this new patchset next week for review.

Ok, I may have missed something. What I meant to say is that I'm using the same namespace functions for switch_creds as all the set*uid syscalls use so what I have should work as well. If one were to pass this fd via CMSG, it could cross a namespace boundary. The changed mappings of this fd would be the same as for any other fd passed across now. Maybe that is irrelevant in this case.

The purpose of the fd is to create a handle to a creds set that a server can then efficiently switch to prior to filesystem syscalls where fsuid etc. are relevant (mkdir, creat, write, etc.). This is exposing the override_creds() and revert_creds() to these servers. I do not intend in our server to hand these fds to anyone else. After all, they are useless for anything other than switch_creds/use_creds.

The server keeps a cache of its known, currently active client creds along with this fd. The fast path is to lookup the creds and use the fd. On cache misses, it does a switch_creds() and saves the fd in the cache.

Quoted 17 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

Another note: I think that there may be issues if the creator of a token
has no_new_privs set and the user doesn't. Imagine a daemon that accepts one of these fds, impersonates it, and calls exec. This could be used to escape from no_new_privs land.

Which is why I was suggesting that we don't allow changing any field in the cred except for uids and gids.

Quoted 6 lines
Quoted 1 line

If the daemon impersonates and execs, we still lose.

I answered this. You only get to impersonate for purposes of file access with reduced caps to prevent you from being root as well. Also, since these are O_CLOEXEC and switch_creds "magic" fds, this can't happen because the fd is gone post-exec.

Quoted 4 lines

If a no_new_privs process authenticates to a daemon and that daemon execs, then there's now a dumpable, ptraceable, non-no-new-privs process running as the uid/gid of the no_new_privs process. This may be dangerous.

Two things. My new patch set now throws an error if the fd is not one returned by switch_creds(). The new syscall here is use_creds() btw. That fd is opened O_CLOEXEC so the syscall has two guards. First, it can only be one that switch_creds() returned and second, it gets closed on an exec so the no_new_privs process doesn't get it. In addition, switch_creds() reduces the effective caps set to the same minimal set that nfsd_setuser() has.

If someone else chooses to pass this fd via CMSG, whoever gets it has reduced privs and in order to use it for anything, i.e. use_creds(), it still needs to inherit SETUID and SETGID caps from its parent or it will get an EPERM error. Passing this in "friendly" code defeats the purpose of the cache above in that we could get fd leaks. If there is another flag that could prevent its being passed along via CMSG, I can add that too because using CMSG is well outside the use case.

Note too, it also cannot pass it on via exec. The use case is and should be as for knfsd, namely which calls nfsd_setuser() followed by fulfilling the protocol op followed by a revert of creds. All this is is in the same thread.

Jim

Quoted 11 lines
Quoted 11 lines
Quoted 2 lines

--Andy

--
Jim Lieb
Linux Systems Engineer
Panasas Inc.

"If ease of use was the only requirement, we would all be riding tricycles"
- Douglas Engelbart 1925–2013

Signature · 6 lines
Jim Lieb
Linux Systems Engineer
Panasas Inc.

"If ease of use was the only requirement, we would all be riding tricycles"
- Douglas Engelbart 1925–2013
#13 lore raw

Re: Re: Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 190 lines · Jim Lieb
Quoted 167 lines · On Thursday, October 31, 2013 12:09:08 Andy Lutomirski wrote:
Quoted 153 lines · Jim Lieb
Quoted 83 lines · On Thursday, October 24, 2013 12:28:15 Andy Lutomirski wrote:

On Wed, Oct 23, 2013 at 10:59 PM, Eric W. Biederman

Quoted 65 lines · <[email protected]> wrote:
Quoted 18 lines · Andy Lutomirski
Quoted 17 lines · Eric W. Biederman
Quoted 4 lines · Al Viro
Quoted 2 lines · Eric W. Biederman

That doesn't look bad but it does need capable(CAP_SETUID) && capable(CAP_SETGID) or possibly something a little more refined.

Quoted 8 lines

D'oh

Quoted 3 lines

I don't think we want file descriptor passing to all of a sudden become
a grant of privilege, beyond what the passed fd can do.

Definitely. And an extra ) to make it compile wouldn't hurt either...

There also appears to need to be a check that we don't gain any capabilities.

Quoted 6 lines
Quoted 2 lines

We also need a check so that you don't gain any capabilities, and possibly a few other things.

Why? I like the user_ns part, but I'm not immediately seeing the issue
with capabilities.

My reasoning was instead of making this syscall as generic as possible start it out by only allowing the cases Jim cares about and working with
a model where you can't gain any permissions you couldn't gain otherwise.

Although the fd -1 trick to revert to your other existing cred seems reasonable.

Quoted 30 lines
Quoted 18 lines

So I suspect we want a check something like:

if ((new_cred->securebits != current_cred->securebits) ||

    (new_cred->cap_inheritable != current_cred->cap_inheritable) ||
    (new_cred->cap_permitted != current_cred->cap_permitted) ||
    (new_cred->cap_effective != current_cred->cap_effective) ||
    (new_cred->cap_bset != current_cred->cap_bset) ||
    (new_cred->jit_keyring != current_cred->jit_keyring) ||
    (new_cred->session_keyring != current_cred->session_keyring) ||
    (new_cred->process_keyring != current_cred->process_keyring) ||
    (new_cred->thread_keyring != current_cred->thread_keyring) ||
    (new_cred->request_keyring != current_cred->request_keyring) ||
    (new_cred->security != current_cred->security) ||
    (new_cred->user_ns != current_cred->user_ns)) {

     return -EPERM;
Quoted 1 line

}

I *really* don't like the idea of being able to use any old file descriptor. I barely care what rights the caller needs to have to invoke this -- if you're going to pass an fd that grants a capability (in the non-Linux sense of the work), please make sure that the sender actually wants that behavior.

IOW, have a syscall to generate a special fd for this purpose. It's only a couple lines of code, and I think we'll really regret it if we fsck this up.

Quoted 7 lines
Quoted 3 lines

(I will take it as a personal challenge to find at least one exploitable
privilege escalation in this if an arbitrary fd works.)

If you can't switch to a uid or a gid you couldn't switch to otherwise then the worst that can happen is an information leak. And information leaks are rarely directly exploitable.

Here's the attack:

Suppose there's a daemon that uses this in conjunction with SCM_RIGHTS. The daemon is effectively root (under the current proposal, it has to be). So a client connects, sends a credential fd, and the daemon impersonates those credentials.

Quoted 8 lines

Now a malicious user obtains *any* fd opened by root. It sends that fd to the daemon. The daemon then impersonates root. We lose. (It can't possibly be hard to obtain an fd with highly privileged f_cred -- I bet that most console programs have stdin like that, for example.

There are probably many setuid programs that will happily open

/dev/null for you, too.)

In my reply to Eric, I note that I need to add a check that the fd passed is one from switch_creds. With that test, not any fd will do and the one that does has only been able to set fsuid, fsgid, altgroups, and reduced (the nfsd set) caps. They can do no more damage than what the original switch_creds allowed. The any fd by root no longer applies so use doesn't get much (no escalation).

Quoted 29 lines
Quoted 24 lines
Quoted 8 lines

Also... real_cred looks confusing. AFAICS it is used *only* for knfsd and faccessat. That is, current userspace can't see it. But now you'll
expose various oddities. For example, AFAICS a capability-less process
that's a userns owner can always use setuid. This will *overwrite* real_cred. Then you're screwed, especially if this happens by accident.

And doing in userland what faccessat, and knfsd do in the kernel is exactly what is desired here. But maybe there are issues with that.

Quoted 11 lines

That being said, Windows has had functions like this for a long time. Processes have a primary token and possibly an impersonation token. Any
process can call ImpersonateLoggedOnUser (no privilege required) to impersonate the credentials of a token (which is special kind of fd). Similarly, any process can call RevertToSelf to undo it.

Is there any actual problem with allowing completely unprivileged tasks
to switch to one of these magic cred fds? That would avoid needing a "revert" operation.

Quoted 4 lines

If the permission model is this switching of credentials doesn't get you
anything you couldn't get some other way. That would seem to totally rules out unprivileged processes switching these things.

Quoted 7 lines

IMO, there are two reasonable models that involve fds carrying some kind of credential.

1. The fd actually carries the ability to use the credentials. You need to be very careful whom you send these to. The security implications are obvious (which is good) and the receiver doesn't need privilege (which is good, as long as the receiver is careful).

I test for caps. I think using switch_creds in all forms should require privs. I thought of the revert case and although it does simply return to "real" creds, you still need CAP_SETUID and CAP_SETGID to do it. This means, of course, if you have really messed up things, you may not be able to get back home which, although a bad thing for the process, is a good thing for the system as a whole.

Quoted 8 lines

2. The fd is for identification only. But this means that the fd carries the ability to identify as a user. So you *still* have to be very careful about whom you send it to. What you need is an operation that allows you to identify using the fd without transitively granting the recipient the same ability. On networks, this is done by signing some kind of challenge. The kernel could work the same way, or there could be a new CMSG_IDENTITY that you need an identity fd to send but that does not copy that fd to the recipient.

Quoted 12 lines

I am not sure I understand this. CMSG only applies to UNIX_DOMAIN sockets which means that the switch_creds fd test still applies here. It is identification but only for within the same kernel. As for namespaces, the
translation was done when the creds fd was created. I suppose if it was passed across namespace boundaries there could be a problem but what is in the creds structure is the translated fduid,fsgid etc., not the untranslated one. We are still doing access checks and quota stuff with the translated creds. If one namespace has "fred" as uid 52 and another has "mary" as 52, the quota will only be credited to whoever "fred" really is only if "fred" gets to be "mary" in the second alternate universe...

Quoted 3 lines

I'm not talking about namespaces here -- I think we're not really on the same page. Can you describe what you're envisioning doing with these fds?

I have a new version that is now two syscalls and no multiplexing has more testing etc. to mirror exactly the tests in setfsuid(). I still am testing but plan to send this new patchset next week for review.

Ok, I may have missed something. What I meant to say is that I'm using the same namespace functions for switch_creds as all the set*uid syscalls use so what I have should work as well. If one were to pass this fd via CMSG, it could cross a namespace boundary. The changed mappings of this fd would be the same as for any other fd passed across now. Maybe that is irrelevant in this case.

The purpose of the fd is to create a handle to a creds set that a server can then efficiently switch to prior to filesystem syscalls where fsuid etc. are relevant (mkdir, creat, write, etc.). This is exposing the override_creds() and revert_creds() to these servers. I do not intend in our server to hand these fds to anyone else. After all, they are useless for anything other than switch_creds/use_creds.

Quoted 3 lines

The server keeps a cache of its known, currently active client creds along with this fd. The fast path is to lookup the creds and use the fd. On cache misses, it does a switch_creds() and saves the fd in the cache.

So if I understand you correctly, you're not planning on sending this fd between process at all. In that case, adding a new API that seems designed for interprocess use and having exactly zero usecases to think about makes me nervous. Why is it going to use fds again?

Or maybe I should wait to see the updated API before complaining :)

Quoted 30 lines
Quoted 18 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines

Another note: I think that there may be issues if the creator of a token
has no_new_privs set and the user doesn't. Imagine a daemon that accepts one of these fds, impersonates it, and calls exec. This could be used to escape from no_new_privs land.

Which is why I was suggesting that we don't allow changing any field in the cred except for uids and gids.

Quoted 6 lines
Quoted 1 line

If the daemon impersonates and execs, we still lose.

I answered this. You only get to impersonate for purposes of file access with reduced caps to prevent you from being root as well. Also, since these are O_CLOEXEC and switch_creds "magic" fds, this can't happen because the fd is gone post-exec.

Quoted 4 lines

If a no_new_privs process authenticates to a daemon and that daemon execs, then there's now a dumpable, ptraceable, non-no-new-privs process running as the uid/gid of the no_new_privs process. This may be dangerous.

Two things. My new patch set now throws an error if the fd is not one returned by switch_creds(). The new syscall here is use_creds() btw. That fd is opened O_CLOEXEC so the syscall has two guards. First, it can only be one that switch_creds() returned and second, it gets closed on an exec so the no_new_privs process doesn't get it. In addition, switch_creds() reduces the effective caps set to the same minimal set that nfsd_setuser() has.

Quoted 7 lines

If someone else chooses to pass this fd via CMSG, whoever gets it has reduced privs and in order to use it for anything, i.e. use_creds(), it still needs to inherit SETUID and SETGID caps from its parent or it will get an EPERM error. Passing this in "friendly" code defeats the purpose of the cache above in that we could get fd leaks. If there is another flag that could prevent its being passed along via CMSG, I can add that too because using CMSG is well outside the use case.

I still don't see the point of lowering effective caps, but this is IMO minor. Are you checking the permitted set on revert?

--Andy

author
#14 lore raw

Re: Re: Re: Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 11 lines · On Thursday, October 31, 2013 12:48:54 Andy Lutomirski wrote:
Quoted 10 lines · Jim Lieb
Quoted 9 lines · On Thursday, October 31, 2013 12:09:08 Andy Lutomirski wrote:
Quoted 8 lines · Jim Lieb
Quoted 7 lines · On Thursday, October 24, 2013 12:28:15 Andy Lutomirski wrote:

On Wed, Oct 23, 2013 at 10:59 PM, Eric W. Biederman

Quoted 4 lines · <[email protected]> wrote:
Quoted 3 lines · Andy Lutomirski
Quoted 2 lines · Eric W. Biederman
Quoted 1 line · Al Viro

On Wed, Oct 16, 2013 at 06:18:16PM -0700, Eric W. Biederman

Quoted 206 lines · wrote:
Quoted 206 lines
Quoted 183 lines
Quoted 166 lines
Quoted 87 lines
Quoted 74 lines
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
Quoted 2 lines

That doesn't look bad but it does need capable(CAP_SETUID) && capable(CAP_SETGID) or possibly something a little more refined.

D'oh

Quoted 3 lines

I don't think we want file descriptor passing to all of a sudden become
a grant of privilege, beyond what the passed fd can do.

Definitely. And an extra ) to make it compile wouldn't hurt either...

There also appears to need to be a check that we don't gain any capabilities.

Quoted 6 lines
Quoted 2 lines

We also need a check so that you don't gain any capabilities, and possibly a few other things.

Why? I like the user_ns part, but I'm not immediately seeing the issue
with capabilities.

My reasoning was instead of making this syscall as generic as possible
start it out by only allowing the cases Jim cares about and working with
a model where you can't gain any permissions you couldn't gain otherwise.

Although the fd -1 trick to revert to your other existing cred seems reasonable.

Quoted 38 lines
Quoted 22 lines

So I suspect we want a check something like:

if ((new_cred->securebits != current_cred->securebits) ||

    (new_cred->cap_inheritable != current_cred->cap_inheritable)
    ||
    (new_cred->cap_permitted != current_cred->cap_permitted) ||
    (new_cred->cap_effective != current_cred->cap_effective) ||
    (new_cred->cap_bset != current_cred->cap_bset) ||
    (new_cred->jit_keyring != current_cred->jit_keyring) ||
    (new_cred->session_keyring != current_cred->session_keyring)
    ||
    (new_cred->process_keyring != current_cred->process_keyring)
    ||
    (new_cred->thread_keyring != current_cred->thread_keyring) ||
    (new_cred->request_keyring != current_cred->request_keyring)
    ||
    (new_cred->security != current_cred->security) ||
    (new_cred->user_ns != current_cred->user_ns)) {
    
     return -EPERM;
Quoted 1 line

}

I *really* don't like the idea of being able to use any old file descriptor. I barely care what rights the caller needs to have to invoke this -- if you're going to pass an fd that grants a capability
(in the non-Linux sense of the work), please make sure that the sender
actually wants that behavior.

IOW, have a syscall to generate a special fd for this purpose. It's
only a couple lines of code, and I think we'll really regret it if we
fsck this up.

Quoted 3 lines

(I will take it as a personal challenge to find at least one exploitable
privilege escalation in this if an arbitrary fd works.)

Quoted 5 lines

If you can't switch to a uid or a gid you couldn't switch to otherwise
then the worst that can happen is an information leak. And information
leaks are rarely directly exploitable.

Here's the attack:

Suppose there's a daemon that uses this in conjunction with SCM_RIGHTS. The daemon is effectively root (under the current proposal, it has to be). So a client connects, sends a credential fd, and the daemon impersonates those credentials.

Quoted 8 lines

Now a malicious user obtains *any* fd opened by root. It sends that fd to the daemon. The daemon then impersonates root. We lose. (It can't possibly be hard to obtain an fd with highly privileged f_cred -- I bet that most console programs have stdin like that, for example.

There are probably many setuid programs that will happily open

/dev/null for you, too.)

In my reply to Eric, I note that I need to add a check that the fd passed
is one from switch_creds. With that test, not any fd will do and the one
that does has only been able to set fsuid, fsgid, altgroups, and reduced
(the nfsd set) caps. They can do no more damage than what the original switch_creds allowed. The any fd by root no longer applies so use doesn't get much (no escalation).

Quoted 34 lines
Quoted 28 lines
Quoted 9 lines

Also... real_cred looks confusing. AFAICS it is used *only* for knfsd
and faccessat. That is, current userspace can't see it. But now you'll
expose various oddities. For example, AFAICS a capability-less process
that's a userns owner can always use setuid. This will *overwrite* real_cred. Then you're screwed, especially if this happens by accident.

And doing in userland what faccessat, and knfsd do in the kernel is exactly what is desired here. But maybe there are issues with that.

Quoted 9 lines

That being said, Windows has had functions like this for a long time.
Processes have a primary token and possibly an impersonation token. Any
process can call ImpersonateLoggedOnUser (no privilege required) to impersonate the credentials of a token (which is special kind of fd).
Similarly, any process can call RevertToSelf to undo it.

Quoted 5 lines

Is there any actual problem with allowing completely unprivileged tasks
to switch to one of these magic cred fds? That would avoid needing a
"revert" operation.

Quoted 5 lines

If the permission model is this switching of credentials doesn't get you
anything you couldn't get some other way. That would seem to totally
rules out unprivileged processes switching these things.

Quoted 7 lines

IMO, there are two reasonable models that involve fds carrying some kind of credential.

1. The fd actually carries the ability to use the credentials. You need to be very careful whom you send these to. The security implications are obvious (which is good) and the receiver doesn't need privilege (which is good, as long as the receiver is careful).

I test for caps. I think using switch_creds in all forms should require
privs. I thought of the revert case and although it does simply return to
"real" creds, you still need CAP_SETUID and CAP_SETGID to do it. This means, of course, if you have really messed up things, you may not be able to get back home which, although a bad thing for the process, is a good thing for the system as a whole.

Quoted 8 lines

2. The fd is for identification only. But this means that the fd carries the ability to identify as a user. So you *still* have to be very careful about whom you send it to. What you need is an operation that allows you to identify using the fd without transitively granting the recipient the same ability. On networks, this is done by signing some kind of challenge. The kernel could work the same way, or there could be a new CMSG_IDENTITY that you need an identity fd to send but that does not copy that fd to the recipient.

Quoted 16 lines

I am not sure I understand this. CMSG only applies to UNIX_DOMAIN sockets
which means that the switch_creds fd test still applies here. It is identification but only for within the same kernel. As for namespaces, the
translation was done when the creds fd was created. I suppose if it was
passed across namespace boundaries there could be a problem but what is in
the creds structure is the translated fduid,fsgid etc., not the untranslated one. We are still doing access checks and quota stuff with the translated creds. If one namespace has "fred" as uid 52 and another
has "mary" as 52, the quota will only be credited to whoever "fred" really is only if "fred" gets to be "mary" in the second alternate universe...

Quoted 3 lines

I'm not talking about namespaces here -- I think we're not really on the same page. Can you describe what you're envisioning doing with these fds?

I have a new version that is now two syscalls and no multiplexing has more testing etc. to mirror exactly the tests in setfsuid(). I still am testing
but plan to send this new patchset next week for review.

Ok, I may have missed something. What I meant to say is that I'm using the same namespace functions for switch_creds as all the set*uid syscalls use so what I have should work as well. If one were to pass this fd via CMSG, it could cross a namespace boundary. The changed mappings of this fd would be the same as for any other fd passed across now. Maybe that is irrelevant in this case.

The purpose of the fd is to create a handle to a creds set that a server can then efficiently switch to prior to filesystem syscalls where fsuid etc. are relevant (mkdir, creat, write, etc.). This is exposing the override_creds() and revert_creds() to these servers. I do not intend in our server to hand these fds to anyone else. After all, they are useless for anything other than switch_creds/use_creds.

Quoted 8 lines
Quoted 3 lines

The server keeps a cache of its known, currently active client creds along with this fd. The fast path is to lookup the creds and use the fd. On cache misses, it does a switch_creds() and saves the fd in the cache.

So if I understand you correctly, you're not planning on sending this fd between process at all. In that case, adding a new API that seems designed for interprocess use and having exactly zero usecases to think about makes me nervous. Why is it going to use fds again?

Correct. They are handles to a set of creds for filesystem calls. I use fds because every filp has a pointer to a set of creds that gets swapped in for various things like coredumps, and in the case of knfsd, impersonating the nfs client for when it does filesystem morphing ops. In order to currently do what nfsd_setuser() does we must do:

	setfsuid();setfsgid(); setgroups(); setcaps();
followed by 
	open/creat/mknod/write
followed by
	 setfsuid(); setfsgid(); setgroups(); setcaps();

Each one of these 4 syscalls discards an RCU copy of the creds, both going in and going out. In the first case below, the revert doesn't recycle the RCU because the open fd still holds a ref. Same applied to the second case. In fact here, no new creds are created either. The creds only get recycled when the fd is closed.

Using an fd to have a handle on what all these do is the simplest thing to pass back and forth between user and kernel space. This is what Ted T'so and Al Viro suggested. This ends up looking like:

	fd = switch_creds(creds struct);
followed by
	open/creat/mknod/write
followed by
	use_creds(-1);

The -1 arg is used as I proposed in the earlier round, namely, it tells use_creds to revert to real creds.

Subsequent uses look like:

	use_creds(cached fd);
followed by
	open/creat/mknod/write
followed by
	use_creds(-1);

There is no need to pass the fd because if the other side has the caps to use the fd, they have the caps to get their own. We are saving overhead.

Quoted 43 lines

Or maybe I should wait to see the updated API before complaining :)

Quoted 32 lines
Quoted 19 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines

Another note: I think that there may be issues if the creator of a token
has no_new_privs set and the user doesn't. Imagine a daemon that accepts one of these fds, impersonates it, and calls exec. This could
be used to escape from no_new_privs land.

Which is why I was suggesting that we don't allow changing any field in
the cred except for uids and gids.

Quoted 7 lines
Quoted 1 line

If the daemon impersonates and execs, we still lose.

I answered this. You only get to impersonate for purposes of file access
with reduced caps to prevent you from being root as well. Also, since these are O_CLOEXEC and switch_creds "magic" fds, this can't happen because the fd is gone post-exec.

Quoted 4 lines

If a no_new_privs process authenticates to a daemon and that daemon execs, then there's now a dumpable, ptraceable, non-no-new-privs process running as the uid/gid of the no_new_privs process. This may be dangerous.

Two things. My new patch set now throws an error if the fd is not one returned by switch_creds(). The new syscall here is use_creds() btw. That fd is opened O_CLOEXEC so the syscall has two guards. First, it can only be one that switch_creds() returned and second, it gets closed on an exec so the no_new_privs process doesn't get it. In addition, switch_creds() reduces the effective caps set to the same minimal set that nfsd_setuser() has.

Quoted 7 lines

If someone else chooses to pass this fd via CMSG, whoever gets it has reduced privs and in order to use it for anything, i.e. use_creds(), it still needs to inherit SETUID and SETGID caps from its parent or it will get an EPERM error. Passing this in "friendly" code defeats the purpose of the cache above in that we could get fd leaks. If there is another flag that could prevent its being passed along via CMSG, I can add that too because using CMSG is well outside the use case.

Quoted 2 lines

I still don't see the point of lowering effective caps, but this is IMO minor. Are you checking the permitted set on revert?

We lower specific filesystem related caps to prevent using the root bypass of acess checks, prevent root bypass of quota restrictions, and to correctly charge the impersonated user for quota'd resource usage. This is what nfsd_setuser() does. I use the same mask set.

I don't check the permitted set associated with the fd because I've already checked that it is a switch_creds() returned fd (they are immutable). I do make a check at the beginning of both syscalls for the caps to set uid/gid, the same test as setfsuid et al.

Quoted 2 lines

--Andy

Signature · 6 lines
Jim Lieb
Linux Systems Engineer
Panasas Inc.

"If ease of use was the only requirement, we would all be riding tricycles"
- Douglas Engelbart 1925–2013
#15 lore raw

Re: Re: Re: Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 7 lines · Jim Lieb

Subsequent uses look like:

	use_creds(cached fd);
followed by
	open/creat/mknod/write
followed by
	use_creds(-1);

Are you aware that calling commit_creds() is prohibitted between override_creds() and revert_creds() ?

If the caller does some operation that calls commit_creds() (like example below), the kernel triggers BUG().

---------- example module start ----------
#include <linux/module.h>
#include <linux/cred.h>
#include <linux/fs.h>
#include <linux/file.h>

static int __init test_init(void)

{
        { /* switch_creds() syscall */
                struct fd f = fdget(0);
                if (!f.file)
                        return -EBADF;
                put_cred(override_creds(f.file->f_cred));
                fdput(f);
        }
        { /* something that calls commit_creds() */
                struct cred *cred = prepare_creds();
                if (cred)
                        commit_creds(cred);
        }
        return 0;
}

static void test_exit(void)
{
}

module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
---------- example module end ----------

Since nobody can guarantee that the caller of switch_creds() never does some operation that calls commit_creds(), I don't think switch_creds() based on override_creds() will work.

author
#16 lore raw

Re: Re: Re: Re: Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 10 lines · On Friday, November 01, 2013 22:24:12 Tetsuo Handa wrote:
Quoted 9 lines · Jim Lieb

Subsequent uses look like:

	use_creds(cached fd);

followed by

open/creat/mknod/write

followed by

Quoted 7 lines
Quoted 1 line
	use_creds(-1);

Are you aware that calling commit_creds() is prohibitted between override_creds() and revert_creds() ?

If the caller does some operation that calls commit_creds() (like example below), the kernel triggers BUG().

Yes, I do. I caught this in an early pass. I only use override_creds() and revert_creds().

Quoted 36 lines

---------- example module start ----------
#include <linux/module.h>
#include <linux/cred.h>
#include <linux/fs.h>
#include <linux/file.h>

static int __init test_init(void)

{
        { /* switch_creds() syscall */
                struct fd f = fdget(0);
                if (!f.file)
                        return -EBADF;
                put_cred(override_creds(f.file->f_cred));
                fdput(f);
        }
        { /* something that calls commit_creds() */
                struct cred *cred = prepare_creds();
                if (cred)
                        commit_creds(cred);
        }
        return 0;
}

static void test_exit(void)
{
}

module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
---------- example module end ----------

Since nobody can guarantee that the caller of switch_creds() never does some operation that calls commit_creds(), I don't think switch_creds() based on override_creds() will work.

Signature · 6 lines
Jim Lieb
Linux Systems Engineer
Panasas Inc.

"If ease of use was the only requirement, we would all be riding tricycles"
- Douglas Engelbart 1925–2013
#17 lore raw

Re: Re: Re: Re: Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 13 lines · Jim Lieb
Quoted 12 lines · On Friday, November 01, 2013 22:24:12 Tetsuo Handa wrote:
Quoted 10 lines · Jim Lieb

Subsequent uses look like:

	use_creds(cached fd);

followed by

open/creat/mknod/write

followed by

	use_creds(-1);
Quoted 8 lines
Quoted 5 lines

Are you aware that calling commit_creds() is prohibitted between override_creds() and revert_creds() ?

If the caller does some operation that calls commit_creds() (like example below), the kernel triggers BUG().

Yes, I do. I caught this in an early pass. I only use override_creds() and revert_creds().

Excuse me, but even below example will trigger BUG(). You pack override_creds() + open() + revert_creds() into one system call so that the caller of this system call shall not do something that calls commit_creds() ?

---------- example module start ----------
#include <linux/module.h>
#include <linux/cred.h>
#include <linux/fs.h>
#include <linux/file.h>

static int __init test_init(void)

{
        const struct cred *orig;
        { /* switch_cred() syscall */
                struct fd f = fdget(0);
                if (!f.file)
                        return -EBADF;
                orig = override_creds(f.file->f_cred);
                fdput(f);
        }
        { /* something that calls commit_creds() */
                struct cred *cred = prepare_creds();
                if (cred)
                        commit_creds(cred);
        }
        { /* restore */
                revert_creds(orig);
        }
        return 0;
}

static void test_exit(void)
{
}

module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
---------- example module end ----------

author
#18 lore raw

Re: Re: Re: Re: Re: Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

Quoted 20 lines · On Saturday, November 02, 2013 01:07:59 Tetsuo Handa wrote:
Quoted 19 lines · Jim Lieb
Quoted 17 lines · On Friday, November 01, 2013 22:24:12 Tetsuo Handa wrote:
Quoted 10 lines · Jim Lieb

Subsequent uses look like:

	use_creds(cached fd);

followed by

open/creat/mknod/write

followed by

	use_creds(-1);

Are you aware that calling commit_creds() is prohibitted between override_creds() and revert_creds() ?

If the caller does some operation that calls commit_creds() (like example below), the kernel triggers BUG().

Quoted 7 lines
Quoted 2 lines

Yes, I do. I caught this in an early pass. I only use override_creds() and revert_creds().

Excuse me, but even below example will trigger BUG(). You pack override_creds() + open() + revert_creds() into one system call so that the caller of this system call shall not do something that calls commit_creds() ?

Ok, I see your point here. If I do a switch_creds and the userland does something like seteuid before I do the revert, we are toast. Correct?

This is an issue. Thanks for pointing this out. It is certainly not in my use case but that doesn't mean someone else won't try it. I have some more work to do.

Quoted 36 lines

---------- example module start ----------
#include <linux/module.h>
#include <linux/cred.h>
#include <linux/fs.h>
#include <linux/file.h>

static int __init test_init(void)

{
        const struct cred *orig;
        { /* switch_cred() syscall */
                struct fd f = fdget(0);
                if (!f.file)
                        return -EBADF;
                orig = override_creds(f.file->f_cred);
                fdput(f);
        }
        { /* something that calls commit_creds() */
                struct cred *cred = prepare_creds();
                if (cred)
                        commit_creds(cred);
        }
        { /* restore */
                revert_creds(orig);
        }
        return 0;
}

static void test_exit(void)
{
}

module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
---------- example module end ----------

Signature · 6 lines
Jim Lieb
Linux Systems Engineer
Panasas Inc.

"If ease of use was the only requirement, we would all be riding tricycles"
- Douglas Engelbart 1925–2013
author
#19 lore raw

Re: Re: [PATCH 1/3] switch_creds: Syscall to switch creds for file server ops

In reply to Eric W. Biederman
Quoted 12 lines · On Wednesday, October 23, 2013 22:59:54 Eric W. Biederman wrote:
Quoted 11 lines · Andy Lutomirski
Quoted 10 lines · Eric W. Biederman
Quoted 9 lines · Al Viro
Quoted 2 lines · Eric W. Biederman

That doesn't look bad but it does need capable(CAP_SETUID) && capable(CAP_SETGID) or possibly something a little more refined.

D'oh

Quoted 2 lines

I don't think we want file descriptor passing to all of a sudden become a grant of privilege, beyond what the passed fd can do.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

Definitely. And an extra ) to make it compile wouldn't hurt either...

There also appears to need to be a check that we don't gain any capabilities.

We also need a check so that you don't gain any capabilities, and possibly a few other things.

My original api does not pass any caps however, the code in setting up the new creds does mask off a number of them in the effective set, the same set that knfsd does. So all I can do is reduce, not extend. We need to reduce caps because without that, quotas and access get bypassed. If I do the generic (4 syscalls), this opens up lots of options for setting things which is not my need or intent.

Quoted 3 lines
Quoted 3 lines

Why? I like the user_ns part, but I'm not immediately seeing the issue with capabilities.

As for name spaces, we don't have much interest here for two reasons.

1. if the server does run under a different namespace, so be it. The syscall does make those translations. If you want to firewall off the service and its exports, fine.

2. in our main use case, nfs-ganesha is running as a pNFS metadata server and its uid namespace comes from somewhere else (krb5 etc.) anyway. The kernel etc. it is running on is completely separate, a black box with its own nsswitch options, probably local only with only a small set of admin entries.

Just to be clear, if there is any mapping between uids within the ganesha env and the local env (an nfs server that is also an app server), all of the above applies assuming the nsswitch/idmapper/ipa is set up properly. The how that happens is outside scope at this level.

Quoted 5 lines

My reasoning was instead of making this syscall as generic as possible start it out by only allowing the cases Jim cares about and working with a model where you can't gain any permissions you couldn't gain otherwise.

Right. That is my intent, to do the same as nfsd_setuser. We need to impersonate the user at the client per the creds we get from the appropriate authentication service and to only do it for file access, nothing more. The api I chose allows later extension but only supports what we need now.

I looked at the case of a setuid or seteuid with this and the use case is NFS/CIFS/9P/... user space file servers. That is why I originally chose an evil multiplexed call. I could split it into two or three syscalls but my intent in either api is to have a restricted set of creds. There is a (potential) new use case in NFSv4.2 labels where we will also have to shove those down as well, adding another variant. We've got to pass them in somehow and the set*uid mess is an example of the other way. The setuid family used to be pretty simple (just two syscalls) but not anymore. At least this is an extensible model that is consistent and returns useful things like EPERM rather than an "alternate" uid/gid that must be interpreted to find the error.

I could also restructure the api to have a typed argument. One early pass had a struct that had everything in it, the op, the fd, and the creds but that has the pitfall of now freezing the whole api at the syscall entry point. If v4.2 labels or CIFS SIDs must be passed in future, there would be no alternative
but yet another syscall. I do validate the arg. The unsigned long does pass
32/64. The typedefs in the struct match what is used by the other syscalls. Our server uses the same typedefs internally so the usual porting issues look minimal (and the same as the setfsuid+setfsgid+setgroups it replaces).

I was also trying to cut down on the number of RCU cleanups here. This is what nfsd_setuser already does. Caching via using an fd as a handle to the kernel resource is good and lightweight but our server under load will have to shed "creds" fds before I/O fds (for lock reasons) and that means the nfsd_setuser method of setting fsuid, fsgid, altgroups, and restricted caps could be the common path under load. RCU is cheaper but it is not cheap and I'd really prefer to not have the fallback make matters worse.

Our use case is a meta-data server part of pNFS server, in particular, pNFS in a clustered env. This means a higher metadata load where these switches have real cost.

Quoted 3 lines

Although the fd -1 trick to revert to your other existing cred seems reasonable.

In my revised api idea, I would still need the revert especially with multiple set* calls that have failure paths. I may not know what was left so a full reset to real is the safest choice in the error path. It is also cheap(er) in the post- file operation return path.

Quoted 62 lines
Quoted 27 lines
Quoted 19 lines

So I suspect we want a check something like:

if ((new_cred->securebits != current_cred->securebits) ||

    (new_cred->cap_inheritable != current_cred->cap_inheritable) ||
    (new_cred->cap_permitted != current_cred->cap_permitted) ||
    (new_cred->cap_effective != current_cred->cap_effective) ||
    (new_cred->cap_bset != current_cred->cap_bset) ||
    (new_cred->jit_keyring != current_cred->jit_keyring) ||
    (new_cred->session_keyring != current_cred->session_keyring) ||
    (new_cred->process_keyring != current_cred->process_keyring) ||
    (new_cred->thread_keyring != current_cred->thread_keyring) ||
    (new_cred->request_keyring != current_cred->request_keyring) ||
    (new_cred->security != current_cred->security) ||
    (new_cred->user_ns != current_cred->user_ns)) {
	
	return -EPERM;
Quoted 1 line

}

I *really* don't like the idea of being able to use any old file descriptor. I barely care what rights the caller needs to have to invoke this -- if you're going to pass an fd that grants a capability (in the non-Linux sense of the work), please make sure that the sender actually wants that behavior.

Quoted 6 lines

IOW, have a syscall to generate a special fd for this purpose. It's only a couple lines of code, and I think we'll really regret it if we fsck this up.

(I will take it as a personal challenge to find at least one exploitable privilege escalation in this if an arbitrary fd works.)

If you can't switch to a uid or a gid you couldn't switch to otherwise then the worst that can happen is an information leak. And information leaks are rarely directly exploitable.

Quoted 6 lines

Also... real_cred looks confusing. AFAICS it is used *only* for knfsd and faccessat. That is, current userspace can't see it. But now you'll expose various oddities. For example, AFAICS a capability-less process that's a userns owner can always use setuid. This will *overwrite* real_cred. Then you're screwed, especially if this happens by accident.

And doing in userland what faccessat, and knfsd do in the kernel is exactly what is desired here. But maybe there are issues with that.

Quoted 9 lines

That being said, Windows has had functions like this for a long time. Processes have a primary token and possibly an impersonation token. Any process can call ImpersonateLoggedOnUser (no privilege required) to impersonate the credentials of a token (which is special kind of fd). Similarly, any process can call RevertToSelf to undo it.

Is there any actual problem with allowing completely unprivileged tasks to switch to one of these magic cred fds? That would avoid needing a "revert" operation.

If the permission model is this switching of credentials doesn't get you anything you couldn't get some other way. That would seem to totally rules out unprivileged processes switching these things.

Quoted 7 lines
Quoted 4 lines

Another note: I think that there may be issues if the creator of a token has no_new_privs set and the user doesn't. Imagine a daemon that accepts one of these fds, impersonates it, and calls exec. This could be used to escape from no_new_privs land.

Which is why I was suggesting that we don't allow changing any field in the cred except for uids and gids.

Yes. Which is why in my original patch I pass a user_creds structure that only has the fsuid, fsgid, and altgroups. This means that the caller can *only* impersonate these particular creds for purposes of file access, not priv escalation.

As for magic fds, I didn't have but should add to the perms check a test to
validate that the passed fd is one of our "magic" ones. The other users of
anon fds do a similar test to verify that the fd is one that they gave you. This check would prevent the using of a random open of /dev/null with unknown caps/creds because it would throw an error, preventing an escalation. This fd
is also opened with O_CLOEXEC so children don't get it. Our use case doesn't
need to fork/exec and if we needed to, we wouldn't be using switch_creds for it anyway. Adding Close-on-exec removes the exploit opportunity.

Jim

Quoted 2 lines

Eric

Signature · 10 lines
Jim Lieb
Linux Systems Engineer
Panasas Inc.

"If ease of use was the only requirement, we would all be riding tricycles"
- Douglas Engelbart 1925–2013
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author
#20 lore raw

PATCH 2/3 switch_creds: Add x86 syscall number

In reply to Jim Lieb

This is temporary number awaiting syscall number assignment.

---
 arch/x86/syscalls/syscall_32.tbl | 1 +
 1 file changed, 1 insertion(+)
arch/x86/syscalls/syscall_32.tbl +1 -0
index aabfb83..b836839 100644
@@ -357,3 +357,4 @@
 348	i386	process_vm_writev	sys_process_vm_writev		compat_sys_process_vm_writev
 349	i386	kcmp			sys_kcmp
 350	i386	finit_module		sys_finit_module
+351	i386	switch_creds		sys_switch_creds
Signature · 1 line
1.8.3.1
author
#21 lore raw

PATCH 3/3 switch_creds: Assign x86_64 syscall number for switch_creds

In reply to Jim Lieb

This is a temporary while waiting for syscall number assignment.

---
 arch/x86/syscalls/syscall_64.tbl | 1 +
 1 file changed, 1 insertion(+)
arch/x86/syscalls/syscall_64.tbl +1 -0
index 38ae65d..f46b75c 100644
@@ -320,6 +320,7 @@
 311	64	process_vm_writev	sys_process_vm_writev
 312	common	kcmp			sys_kcmp
 313	common	finit_module		sys_finit_module
+314	common	switch_creds		sys_switch_creds
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
Signature · 1 line
1.8.3.1