Skip to contentSkip to conversation

c/r: Do not crash if socket has no peercred

PATCH In review Dan Smith 2 participants 3 messages Feb 7, 2011 – Mar 26, 2011 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Dan Smith

author
#0 lore raw

PATCH 1/2 c/r: Do not crash if socket has no peercred

Unconnected sockets don't have a valid sk_peercred yet. If we checkpoint them, we'll segv on the NULL pointer.

---
 net/unix/checkpoint.c |   52 +++++++++++++++++++++++++++---------------------
 1 files changed, 29 insertions(+), 23 deletions(-)
net/unix/checkpoint.c +29 -23
index 708df40..25464cc 100644
@@ -159,16 +159,19 @@ int unix_checkpoint(struct ckpt_ctx *ctx, struct socket *sock)
 		goto out;
 	}
 
-	/*
-	 * intentionally drop 'const' qualifier for checkpoint_obj() to
-	 * increment the usage count - it does not alter the credentials.
-	 */
-	cred = (struct cred *) get_cred(sock->sk->sk_peer_cred);
-	un->peercred = checkpoint_obj(ctx, cred, CKPT_OBJ_CRED);
-	put_cred(cred);
-	if (un->peercred < 0) {
-		ret = un->peercred;
-		goto out;
+	if (sock->sk->sk_peer_cred) {
+		/*
+		 * intentionally drop 'const' qualifier for
+		 * checkpoint_obj() to increment the usage count - it
+		 * does not alter the credentials.
+		 */
+		cred = (struct cred *) get_cred(sock->sk->sk_peer_cred);
+		un->peercred = checkpoint_obj(ctx, cred, CKPT_OBJ_CRED);
+		put_cred(cred);
+		if (un->peercred < 0) {
+			ret = un->peercred;
+			goto out;
+		}
 	}
 
 	ret = ckpt_write_obj(ctx, (struct ckpt_hdr *) un);
@@ -438,19 +441,22 @@ static int unix_restore_connected(struct ckpt_ctx *ctx,
 		addrlen = un->laddr_len;
 	}
 
-	cred = ckpt_obj_fetch(ctx, un->peercred, CKPT_OBJ_CRED);
-	if (!cred) {
-		ckpt_err(ctx, -EINVAL, "%(O)Bad peer cred\n", un->peercred);
-		return -EINVAL;
-	}
-
-	if (may_setuid(ctx->realcred->user->user_ns, cred->uid) &&
-	    may_setgid(cred->gid)) {
-		set_peercred(sk, task_tgid(current), cred);
-	} else {
-		ckpt_err(ctx, -EPERM, "peercred %i:%i would require setuid",
-			 cred->uid, cred->gid);
-		return -EPERM;
+	if (un->peercred) {
+		cred = ckpt_obj_fetch(ctx, un->peercred, CKPT_OBJ_CRED);
+		if (!cred) {
+			ckpt_err(ctx, -EINVAL,
+				 "%(O)Bad peer cred\n", un->peercred);
+			return -EINVAL;
+		}
+		if (may_setuid(ctx->realcred->user->user_ns, cred->uid) &&
+		    may_setgid(cred->gid)) {
+			set_peercred(sk, task_tgid(current), cred);
+		} else {
+			ckpt_err(ctx, -EPERM,
+				 "peercred %i:%i would require setuid",
+				 cred->uid, cred->gid);
+			return -EPERM;
+		}
 	}
 
 	if (!dead && (un->peer > 0)) {
Signature · 1 line
1.7.2.2

A message that is not in the archive (sent off-list or not yet received): 1297096979-23145-1-git-send-email-danms-r/[email protected]

author
#2 lore raw

PATCH 2/2 c/r: Check for bound AF_UNIX sockets before doing unlink()

If we're not restarting inside our own network namespace, the explicit unlink() of the AF_UNIX socket path before binding is not safe. This could end up hosing something that has that socket open. The easiest way to encounter such a problem is to checkpoint and restart an application with a connection to syslogd. Without this patch, on restart you hose the system syslogd by unlink()'ing its /dev/log.

We hold the mutex on the parent inode to make sure that we don't race with another unix_bind() in progress.

---
 include/net/af_unix.h |    1 +
 net/unix/af_unix.c    |    2 +-
 net/unix/checkpoint.c |   55 +++++++++++++++++++++++-------------------------
 3 files changed, 28 insertions(+), 30 deletions(-)
include/net/af_unix.h +1 -0
index add734f..a24a842 100644
@@ -11,6 +11,7 @@ extern void unix_notinflight(struct file *fp);
 extern void unix_gc(void);
 extern void wait_for_unix_gc(void);
 extern struct sock *unix_get_socket(struct file *filp);
+extern struct sock *unix_find_socket_byinode(struct inode *i);
 
 #define UNIX_HASH_SIZE	256
 
net/unix/af_unix.c +1 -1
index cb7afd5..e385a1e 100644
@@ -282,7 +282,7 @@ static inline struct sock *unix_find_socket_byname(struct net *net,
 	return s;
 }
 
-static struct sock *unix_find_socket_byinode(struct inode *i)
+struct sock *unix_find_socket_byinode(struct inode *i)
 {
 	struct sock *s;
 	struct hlist_node *node;
net/unix/checkpoint.c +26 -29

81 lines not on this page · raw message

Signature · 1 line
1.7.2.2
#3 lore raw
In reply to a message not in the archive

Pulled for ckpt-v23-rc2.

Oren.

Quoted 78 lines · Dan Smith

Unconnected sockets don't have a valid sk_peercred yet. If we checkpoint them, we'll segv on the NULL pointer.

---
 net/unix/checkpoint.c |   52 +++++++++++++++++++++++++++---------------------
 1 files changed, 29 insertions(+), 23 deletions(-)
net/unix/checkpoint.c +29 -23
index 708df40..25464cc 100644
@@ -159,16 +159,19 @@ int unix_checkpoint(struct ckpt_ctx *ctx, struct socket *sock)
 		goto out;
 	}
 
-	/*
-	 * intentionally drop 'const' qualifier for checkpoint_obj() to
-	 * increment the usage count - it does not alter the credentials.
-	 */
-	cred = (struct cred *) get_cred(sock->sk->sk_peer_cred);
-	un->peercred = checkpoint_obj(ctx, cred, CKPT_OBJ_CRED);
-	put_cred(cred);
-	if (un->peercred < 0) {
-		ret = un->peercred;
-		goto out;
+	if (sock->sk->sk_peer_cred) {
+		/*
+		 * intentionally drop 'const' qualifier for
+		 * checkpoint_obj() to increment the usage count - it
+		 * does not alter the credentials.
+		 */
+		cred = (struct cred *) get_cred(sock->sk->sk_peer_cred);
+		un->peercred = checkpoint_obj(ctx, cred, CKPT_OBJ_CRED);
+		put_cred(cred);
+		if (un->peercred < 0) {
+			ret = un->peercred;
+			goto out;
+		}
 	}
 
 	ret = ckpt_write_obj(ctx, (struct ckpt_hdr *) un);
@@ -438,19 +441,22 @@ static int unix_restore_connected(struct ckpt_ctx *ctx,
 		addrlen = un->laddr_len;
 	}
 
-	cred = ckpt_obj_fetch(ctx, un->peercred, CKPT_OBJ_CRED);
-	if (!cred) {
-		ckpt_err(ctx, -EINVAL, "%(O)Bad peer cred\n", un->peercred);
-		return -EINVAL;
-	}
-
-	if (may_setuid(ctx->realcred->user->user_ns, cred->uid) &&
-	    may_setgid(cred->gid)) {
-		set_peercred(sk, task_tgid(current), cred);
-	} else {
-		ckpt_err(ctx, -EPERM, "peercred %i:%i would require setuid",
-			 cred->uid, cred->gid);
-		return -EPERM;
+	if (un->peercred) {
+		cred = ckpt_obj_fetch(ctx, un->peercred, CKPT_OBJ_CRED);
+		if (!cred) {
+			ckpt_err(ctx, -EINVAL,
+				 "%(O)Bad peer cred\n", un->peercred);
+			return -EINVAL;
+		}
+		if (may_setuid(ctx->realcred->user->user_ns, cred->uid) &&
+		    may_setgid(cred->gid)) {
+			set_peercred(sk, task_tgid(current), cred);
+		} else {
+			ckpt_err(ctx, -EPERM,
+				 "peercred %i:%i would require setuid",
+				 cred->uid, cred->gid);
+			return -EPERM;
+		}
 	}
 
 	if (!dead && (un->peer > 0)) {