c/r: Do not crash if socket has no peercred
Subsystems (2)
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
@@ -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]
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
@@ -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
@@ -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
Pulled for ckpt-v23-rc2.
Oren.
Quoted 78 lines · Dan Smith
Unconnected sockets don't have a valid
sk_peercredyet. If we checkpoint them, we'll segv on the NULL pointer.Signed-off-by Dan Smith <danms-r/[email protected]>--- net/unix/checkpoint.c | 52 +++++++++++++++++++++++++++--------------------- 1 files changed, 29 insertions(+), 23 deletions(-)net/unix/checkpoint.c +29
-23@@ -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 )) {