C/R support of UNIX sockets
Subsystems (3)
PATCH 3/5 Add common socket helpers to unify the security hooks
In reply to Dan SmithThis moves the meat out of the bind(), getsockname(), and getpeername() syscalls into helper functions that performs security_socket_bind() and then the sock->ops->call(). This allows a unification of this behavior between the syscalls and the pending socket restart logic.
--- include/net/sock.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ net/socket.c | 29 ++++++----------------------- 2 files changed, 54 insertions(+), 23 deletions(-)
include/net/sock.h +48 -0
@@ -1572,6 +1572,54 @@ extern void sock_enable_timestamp(struct sock *sk, int flag);extern int sock_get_timestamp (struct sock *,struct timeval __user *);extern int sock_get_timestampns (struct sock *,struct timespec __user *); +/* bind() helper shared between any callers needing to perform a bind on +* behalf of userspace (syscall and restart) with the security hooks. +*/ +static inline int sock_bind (struct socket *sock, +struct sockaddr *addr, +int addr_len) +{ +int err; + + err =security_socket_bind (sock, addr, addr_len); +if (err) +return err; +else +return sock->ops->bind (sock, addr, addr_len); +} + +/* getname() helper shared between any callers needing to perform a getname on +* behalf of userspace (syscall and restart) with the security hooks. +*/ +static inline int sock_getname (struct socket *sock, +struct sockaddr *addr, +int *addr_len) +{ +int err; + + err =security_socket_getsockname (sock); +if (err) +return err; +else +return sock->ops->getname (sock, addr, addr_len,0 ); +} + +/* getpeer() helper shared between any callers needing to perform a getpeer on +* behalf of userspace (syscall and restart) with the security hooks. +*/ +static inline int sock_getpeer (struct socket *sock, +struct sockaddr *addr, +int *addr_len) +{ +int err; + + err =security_socket_getpeername (sock); +if (err) +return err; +else +return sock->ops->getname (sock, addr, addr_len,1 ); +} +/* * Enable debug/info messages */
net/socket.c +6 -23
@@ -1414,15 +1414,10 @@ SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen) sock =sockfd_lookup_light (fd, &err, &fput_needed);if (sock) { err =move_addr_to_kernel (umyaddr, addrlen, (struct sockaddr *)&address); -if (err >=0 ) { - err =security_socket_bind (sock, - (struct sockaddr *)&address, - addrlen); -if (!err) - err = sock->ops->bind (sock, - (struct sockaddr *) - &address, addrlen); - } +if (err >=0 ) + err =sock_bind (sock, + (struct sockaddr *)&address, + addrlen);fput_light (sock->file, fput_needed); }return err;
@@ -1610,11 +1605,7 @@ SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,if (!sock) goto out; - err =security_socket_getsockname (sock); -if (err) - goto out_put; - - err = sock->ops->getname (sock, (struct sockaddr *)&address, &len,0 ); + err =sock_getname (sock, (struct sockaddr *)&address, &len);if (err) goto out_put; err =move_addr_to_user ((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
@@ -1639,15 +1630,7 @@ SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr, sock =sockfd_lookup_light (fd, &err, &fput_needed);if (sock !=NULL ) { - err =security_socket_getpeername (sock); -if (err) { -fput_light (sock->file, fput_needed); -return err; - } - - err = - sock->ops->getname (sock, (struct sockaddr *)&address, &len, -1 ); + err =sock_getpeer (sock, (struct sockaddr *)&address, &len);if (!err) err =move_addr_to_user ((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
Signature · 1 line
1.6.2.5
PATCH 5/5 c/r: Add AF_UNIX support (v7)
In reply to Dan SmithThis patch adds basic checkpoint/restart support for AF_UNIX sockets. It has been tested with a single and multiple processes, and with data inflight at the time of checkpoint. It supports socketpair()s, path-based, and abstract sockets.
Changes in v7:
- Fix failure to free iov_base in error path of sock_read_buffer()
- Change sock_read_buffer() to use _ckpt_read_obj_type() to get the header length and then use ckpt_kread() directly to read the payload
- Change sock_read_buffers() to sock_unix_read_buffers() and break out some common functionality to better accommodate the subsequent INET patch
- Generalize sock_unix_getnames() into sock_getnames() so INET can use it
- Change skb_morph() to skb_clone() which uses the more common path and still avoids the copy
- Add check to validate the socket type before creating socket on restore
- Comment the CAP_NET_ADMIN override in sock_read_buffer_hdr
- Strengthen the comment about priming the buffer limits
- Change the objhash functions to deny direct checkpoint of sockets and remove the reference counting function
- Change SOCKET_BUFFERS to SOCKET_QUEUE
- Change this,peer objrefs to signed integers
- Remove names from internal socket structures
- Fix handling of sock_copy_buffers() result
- Use ckpt_fill_fname() instead of d_path() for writing CWD
- Use sock_getname() and sock_getpeer() for proper security hookage
- Return -ENOSYS for unsupported socket families in checkpoint and restart
- Use sock_setsockopt() and sock_getsockopt() where possible to save and restore socket option values
- Check for SOCK_DESTROY flag in the global verify function because none of our supported socket types use it
- Check for SOCK_USE_WRITE_QUEUE in AF_UNIX restore function because that flag should not be used on such a socket
- Check socket state in UNIX restart path to validate the subset of valid values
Changes in v6:
- Moved the socket addresses to the per-type header
- Eliminated the HASCWD flag
- Remove use of ckpt_write_err() in restart paths
- Change the order in which buffers are read so that we can set the socket's limit equal to the size of the image's buffers (if appropriate) and then restore the original values afterwards.
- Use the ckpt_validate_errno() helper
- Add a check to make sure that we didn't restore a (UNIX) socket with any skb's in the send buffer
- Fix up sock_unix_join() to not leave addr uninitialized for socketpair
- Remove inclusion of checkpoint_hdr.h in the socket files
- Make sock_unix_write_cwd() use ckpt_write_string() and use the new ckpt_read_string() for reading the cwd
- Use the restored realcred credentials in sock_unix_join()
- Fix error path of the chdir_and_bind
- Change the algorithm for reloading the socket buffers to use sendmsg() on the socket's peer for better accounting
- For DGRAM sockets, check the backlog value against the system max to avoid letting a restart bypass the overloaded queue length
- Use sock_bind() instead of sock->ops->bind() to gain the security hook
- Change "restart" to "restore" in some of the function names
Changes in v5:
- Change laddr and raddr buffers in socket header to be long enough for INET6 addresses
- Place socket.c and sock.h function definitions inside #ifdef CONFIG_CHECKPOINT
- Add explicit check in sock_unix_makeaddr() to refuse if the checkpoint image specifies an addr length of 0
- Split sock_unix_restart() into a few pieces to facilitate:
- Changed behavior of the unix restore code so that unlinked LISTEN sockets don't do a bind()...unlink()
- Save the base path of a bound socket's path so that we can chdir() to the base before bind() if it is a relative path
- Call bind() for any socket that is not established but has a non-zero-length local address
- Enforce the current sysctl limit on socket buffer size during restart unless the user holds CAP_NET_ADMIN
- Unlink a path-based socket before calling bind()
Changes in v4:
- Changed the signdness of rcvlowat, rcvtimeo, sndtimeo, and backlog to match their struct sock definitions. This should avoid issues with sign extension.
- Add a sock_cptrst_verify() function to be run at restore time to validate several of the values in the checkpoint image against limits, flag masks, etc.
- Write an error string with ctk_write_err() in the obscure cases
- Don't write socket buffers for listen sockets
- Sanity check address lengths before we agree to allocate memory
- Check the result of inserting the peer object in the objhash on restart
- Check return value of sock_cptrst() on restart
- Change logic in remote getname() phase of checkpoint to not fail for closed (et al) sockets
- Eliminate the memory copy while reading socket buffers on restart
Changes in v3:
- Move sock_file_checkpoint() above sock_file_restore()
- Change __sock_file_*() functions to do_sock_file_*()
- Adjust some of the struct cr_hdr_socket alignment
- Improve the sock_copy_buffers() algorithm to avoid locking the source queue for the entire operation
- Fix alignment in the socket header struct(s)
- Move the per-protocol structure (ckpt_hdr_socket_un) out of the common socket header and read/write it separately
- Fix missing call to sock_cptrst() in restore path
- Break out the socket joining into another function
- Fix failure to restore the socket address thus fixing getname()
- Check the state values on restart
- Fix case of state being TCP_CLOSE, which allows dgram sockets to be properly connected (if appropriate) to their peer and maintain the sockaddr for getname() operation
- Fix restoring a listening socket that has been unlink()'d
- Fix checkpointing sockets with an in-flight FD-passing SKB. Fail with EBUSY.
- Fix checkpointing listening sockets with an unaccepted connection. Fail with EBUSY.
- Changed 'un' to 'unix' in function and structure names
Changes in v2:
- Change GFP_KERNEL to GFP_ATOMIC in sock_copy_buffers() (this seems to be rather common in other uses of skb_copy())
- Move the ckpt_hdr_socket structure definition to linux/socket.h
- Fix whitespace issue
- Move sock_file_checkpoint() to net/socket.c for symmetry
--- checkpoint/files.c | 7 + checkpoint/objhash.c | 19 + include/linux/checkpoint_hdr.h | 12 + include/linux/socket.h | 65 +++ include/net/sock.h | 11 + net/Makefile | 2 + net/checkpoint.c | 894 ++++++++++++++++++++++++++++++++++++++++ net/socket.c | 85 ++++ 8 files changed, 1095 insertions(+), 0 deletions(-) create mode 100644 net/checkpoint.c
checkpoint/files.c +7 -0
@@ -21,6 +21,7 @@#include <linux/syscalls.h> #include <linux/checkpoint.h> #include <linux/checkpoint_hdr.h> +#include <net/sock.h> /**************************************************************************
@@ -548,6 +549,12 @@ static struct restore_file_ops restore_file_ops[] = { .file_type = CKPT_FILE_PIPE, .restore = pipe_file_restore, }, +/* socket */ + { + .file_name = "SOCKET", + .file_type = CKPT_FILE_SOCKET, + .restore = sock_file_restore, + }, };static struct file *do_restore_file (struct ckpt_ctx *ctx)
checkpoint/objhash.c +19 -0
@@ -20,6 +20,7 @@#include <linux/user_namespace.h> #include <linux/checkpoint.h> #include <linux/checkpoint_hdr.h> +#include <net/sock.h> struct ckpt_obj ;struct ckpt_obj_ops ;
@@ -244,6 +245,17 @@ static void obj_groupinfo_drop(void *ptr)put_group_info ((struct group_info *) ptr); } +static int obj_sock_grab (void *ptr) +{ +sock_hold ((struct sock *) ptr); +return 0 ; +} + +static void obj_sock_drop (void *ptr) +{ +sock_put ((struct sock *) ptr); +} +static struct ckpt_obj_ops ckpt_obj_ops[] = {/* ignored object */ {
@@ -367,6 +379,13 @@ static struct ckpt_obj_ops ckpt_obj_ops[] = { .checkpoint = checkpoint_groupinfo, .restore = restore_groupinfo, }, +/* sock object */ + { + .obj_name = "SOCKET", + .obj_type = CKPT_OBJ_SOCK, + .ref_drop = obj_sock_drop, + .ref_grab = obj_sock_grab, + }, };
include/linux/checkpoint_hdr.h +12 -0
@@ -88,6 +88,12 @@ enum {CKPT_HDR_SIGHAND =601 , +CKPT_HDR_FD_SOCKET =701 , +CKPT_HDR_SOCKET , +CKPT_HDR_SOCKET_QUEUE , +CKPT_HDR_SOCKET_BUFFER , +CKPT_HDR_SOCKET_UNIX , +CKPT_HDR_TAIL =9001 , CKPT_HDR_ERROR = 9999,
@@ -122,6 +128,7 @@ enum obj_type {CKPT_OBJ_CRED ,CKPT_OBJ_USER ,CKPT_OBJ_GROUPINFO , +CKPT_OBJ_SOCK , CKPT_OBJ_MAX };
@@ -326,6 +333,7 @@ enum file_type {CKPT_FILE_IGNORE =0 ,CKPT_FILE_GENERIC ,CKPT_FILE_PIPE , +CKPT_FILE_SOCKET , CKPT_FILE_MAX };
@@ -349,6 +357,10 @@ struct ckpt_hdr_file_pipe {__s32 pipe_objref; } __attribute__((aligned(8))); +struct ckpt_hdr_file_socket { +struct ckpt_hdr_file common; +} __attribute__((aligned (8 ))); +struct ckpt_hdr_utsns {struct ckpt_hdr h;char sysname[__NEW_UTS_LEN +1 ];
include/linux/socket.h +65 -0
76 lines not on this page · raw message
include/net/sock.h +11 -0
@@ -1640,4 +1640,15 @@ extern int sysctl_optmem_max;extern __u32 sysctl_wmem_default;extern __u32 sysctl_rmem_default; +#ifdef CONFIG_CHECKPOINT +/* Checkpoint/Restart Functions */ +struct ckpt_ctx ; +struct ckpt_hdr_socket ; +extern int sock_file_checkpoint (struct ckpt_ctx *,void *); +extern void *sock_file_restore (struct ckpt_ctx *); +extern struct socket *do_sock_file_restore (struct ckpt_ctx *, +struct ckpt_hdr_socket *); +extern int do_sock_file_checkpoint (struct ckpt_ctx *ctx,struct file *file); +#endif +#endif /* _SOCK_H */
net/Makefile +2 -0
@@ -66,3 +66,5 @@ ifeq ($(CONFIG_NET),y) obj-$(CONFIG_SYSCTL) += sysctl_net.o endif obj-$(CONFIG_WIMAX) += wimax/ + +obj-$(CONFIG_CHECKPOINT) += checkpoint.o
net/checkpoint.c new file +894 -0
894 lines not on this page · raw message
net/socket.c +85 -0
103 lines not on this page · raw message
Signature · 1 line
1.6.2.5
This is my latest series of patches supporting the goal of checkpoint/restart of AF_UNIX sockets. Changes are detailed per-patch, but a brief description is:
- Update the errno helper patch to compaire against MAX_ERRNO
- Update read_string() patch to use new _ckpt_read_obj_type() behavior and avoid a memory copy
- Update the sock_bind() patch to include similar wrappers for sock_getname() and sock_getpeer()
- Add a patch to export ckpt_fill_fname()
- Update the AF_UNIX patch to integrate received feedback
Can some or all of these patches be included in the tree at this point? I think that some of the helper items should be ready for that, and pulling in the UNIX patch will help make further changes more clear instead of me just posting the large patch each time. Thoughts?
PATCH 1/5 Add an errno validation function (v2)
In reply to a message not in the archiveChanges in v2:
- Compare against MAX_ERRNO instead of more stringent but obscure constant
--- include/linux/checkpoint.h | 6 +++++- 1 files changed, 5 insertions(+), 1 deletions(-)
include/linux/checkpoint.h +5 -1
@@ -28,7 +28,7 @@#include <linux/user_namespace.h> #include <linux/checkpoint_types.h> #include <linux/checkpoint_hdr.h> - +#include <linux/err.h> /* ckpt_ctx: kflags */ #define CKPT_CTX_CHECKPOINT_BIT 0
@@ -257,6 +257,10 @@ extern void *restore_sighand(struct ckpt_ctx *ctx);memcpy (LIVE ,SAVE , count *sizeof (*SAVE )); \ }while (0 ) +static inline int ckpt_validate_errno(int errno) +{ +return (errno >=0 ) && (errno <MAX_ERRNO ); +}/* debugging flags */ #define CKPT_DBASE 0x1/* anything */
Signature · 1 line
1.6.2.5
PATCH 2/5 Add a ckpt_read_string() function (v2)
In reply to a message not in the archiveAdd a ckpt_read_string() function to allow reading of a variable-length (but length-capped) string from the checkpoint stream.
Changes in v2:
- Avoid memcpy() by reading into the allocated buffer directly
--- checkpoint/restart.c | 36 ++++++++++++++++++++++++++++++++++++ include/linux/checkpoint.h | 1 + 2 files changed, 37 insertions(+), 0 deletions(-)
checkpoint/restart.c +36 -0
@@ -285,6 +285,42 @@ int ckpt_read_consume(struct ckpt_ctx *ctx, int len, int type)return ret; } +/** +* ckpt_read_string - read a string (variable length) +* @ctx: checkpoint context +* @str: pointer to buffer to store allocated string (caller must kfree()) +* @max: maximum acceptable length +* +* This can be used to read a variable-length string from the checkpoint +* stream. @max limits the size of the resulting buffer. Returns zero on +* success, negative on failure. +*/ +int ckpt_read_string (struct ckpt_ctx *ctx,char **str,int max) +{ +int len; +int ret =0 ; + + *str =NULL ; + + len =_ckpt_read_obj_type (ctx,NULL ,0 ,CKPT_HDR_STRING ); +if (len <0 ) +return len; +else if (len > max) +return -EINVAL ; + + *str =kzalloc (len +1 ,GFP_KERNEL ); +if (!*str) + ret = -ENOMEM ; + + ret =ckpt_kread (ctx, *str, len); +if (ret <0 ) { +kfree (*str); + *str =NULL ; + } + +return ret; +} +/*********************************************************************** * Restart */
include/linux/checkpoint.h +1 -0
@@ -68,6 +68,7 @@ extern int _ckpt_read_obj_type(struct ckpt_ctx *ctx,extern int _ckpt_read_nbuffer (struct ckpt_ctx *ctx,void *ptr,int len);extern int _ckpt_read_buffer (struct ckpt_ctx *ctx,void *ptr,int len);extern int _ckpt_read_string (struct ckpt_ctx *ctx,void *ptr,int len); +extern int ckpt_read_string (struct ckpt_ctx *ctx,char **str,int max);extern void *ckpt_read_obj_type (struct ckpt_ctx *ctx,int len,int type);extern void *ckpt_read_buf_type (struct ckpt_ctx *ctx,int len,int type);extern int ckpt_read_consume (struct ckpt_ctx *ctx,int len,int type);
Signature · 1 line
1.6.2.5
PATCH 4/5 Export fill_fname() as ckpt_fill_fname()
In reply to a message not in the archive--- checkpoint/files.c | 8 ++++---- include/linux/checkpoint.h | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-)
checkpoint/files.c +4 -4
@@ -28,14 +28,14 @@*/ /** -* fill_fname - return pathname of a given file +* ckpt_fill_fname - return pathname of a given file * @path: path name * @root: relative root * @buf: buffer for pathname * @len: buffer length (in) and pathname length (out) */ -static char *fill_fname (struct path *path,struct path *root, -char *buf,int *len) +char *ckpt_fill_fname (struct path *path,struct path *root, +char *buf,int *len) {struct path tmp = *root;char *fname;
@@ -79,7 +79,7 @@ int checkpoint_fname(struct ckpt_ctx *ctx, struct path *path, struct path *root)if (!buf)return -ENOMEM ; - fname =fill_fname (path, root, buf, &flen); + fname =ckpt_fill_fname (path, root, buf, &flen);if (!IS_ERR (fname)) ret =ckpt_write_obj_type (ctx, fname, flen,CKPT_HDR_FILE_NAME );
include/linux/checkpoint.h +2 -0
@@ -72,6 +72,8 @@ extern int ckpt_read_string(struct ckpt_ctx *ctx, char **str, int max);extern void *ckpt_read_obj_type (struct ckpt_ctx *ctx,int len,int type);extern void *ckpt_read_buf_type (struct ckpt_ctx *ctx,int len,int type);extern int ckpt_read_consume (struct ckpt_ctx *ctx,int len,int type); +extern char *ckpt_fill_fname (struct path *path,struct path *root, +char *buf,int *len);/* ckpt kflags */ #define ckpt_set_ctx_kflag (__ctx, __kflag) \
Signature · 1 line
1.6.2.5
Re: [PATCH 1/5] Add an errno validation function (v2)
In reply to a message not in the archiveQuoted 23 lines · Dan Smith
Signed-off-by Dan Smith <danms-r/[email protected]>Changes in v2:
- Compare againstMAX_ERRNOinstead of more stringent but obscure constant--- include/linux/checkpoint.h | 6 +++++- 1 files changed, 5 insertions(+), 1 deletions(-)include/linux/checkpoint.h +1
-1@@ -28,7 +28,7 @@#include <linux/user_namespace.h> #include <linux/checkpoint_types.h> #include <linux/checkpoint_hdr.h> - +#include <linux/err.h> /* ckpt_ctx: kflags */ #define CKPT_CTX_CHECKPOINT_BIT 0
include/linux/checkpoint.h extern void *restore_sighand(struct ckpt_ctx *ctx); · line 263
Quoted 7 lines
include/linux/checkpoint.h +4
-0@@ -257,6 +257,10 @@ extern void *restore_sighand(struct ckpt_ctx *ctx);memcpy (LIVE ,SAVE , count *sizeof (*SAVE )); \ }while (0 ) +static inline int ckpt_validate_errno(int errno) +{ +return (errno >=0 ) && (errno <MAX_ERRNO ); +}
Maybe i should take a break and re-read in a few minutes, but don't you want this to be:
return (errno > 0) && (errno <= MAX_ERRNO));
Quoted 10 lines
include/linux/checkpoint.h +0
-0/* debugging flags */ #define CKPT_DBASE 0x1/* anything */ Signature · 6 lines
1.6.2.5 _______________________________________________ Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org https://lists.linux-foundation.org/mailman/listinfo/containers
Re: [PATCH 2/5] Add a ckpt_read_string() function (v2)
In reply to a message not in the archiveQuoted 36 lines · Dan Smith
Add a
ckpt_read_string()function to allow reading of a variable-length (but length-capped) string from the checkpoint stream.Changes in v2:
- Avoidmemcpy()by reading into the allocated buffer directlySigned-off-by Dan Smith <danms-r/[email protected]>--- checkpoint/restart.c | 36 ++++++++++++++++++++++++++++++++++++ include/linux/checkpoint.h | 1 + 2 files changed, 37 insertions(+), 0 deletions(-)checkpoint/restart.c +16
-0@@ -285,6 +285,42 @@ int ckpt_read_consume(struct ckpt_ctx *ctx, int len, int type)return ret; } +/** +* ckpt_read_string - read a string (variable length) +* @ctx: checkpoint context +* @str: pointer to buffer to store allocated string (caller must kfree()) +* @max: maximum acceptable length +* +* This can be used to read a variable-length string from the checkpoint +* stream. @max limits the size of the resulting buffer. Returns zero on +* success, negative on failure. +*/ +int ckpt_read_string (struct ckpt_ctx *ctx,char **str,int max) +{ +int len; +int ret =0 ; + + *str =NULL ;
checkpoint/restart.c int ckpt_read_consume(struct ckpt_ctx *ctx, int len, int type) · line 313
Quoted 10 lines
checkpoint/restart.c +10
-0+ + len =_ckpt_read_obj_type (ctx,NULL ,0 ,CKPT_HDR_STRING ); +if (len <0 ) +return len; +else if (len > max) +return -EINVAL ; + + *str =kzalloc (len +1 ,GFP_KERNEL ); +if (!*str) + ret = -ENOMEM ;
would you like to return here as well?
Quoted 32 lines
checkpoint/restart.c +10
-0+ + ret =ckpt_kread (ctx, *str, len); +if (ret <0 ) { +kfree (*str); + *str =NULL ; + } + +return ret; +} +/*********************************************************************** * Restart */ include/linux/checkpoint.h +1
-0@@ -68,6 +68,7 @@ extern int _ckpt_read_obj_type(struct ckpt_ctx *ctx,extern int _ckpt_read_nbuffer (struct ckpt_ctx *ctx,void *ptr,int len);extern int _ckpt_read_buffer (struct ckpt_ctx *ctx,void *ptr,int len);extern int _ckpt_read_string (struct ckpt_ctx *ctx,void *ptr,int len); +extern int ckpt_read_string (struct ckpt_ctx *ctx,char **str,int max);extern void *ckpt_read_obj_type (struct ckpt_ctx *ctx,int len,int type);extern void *ckpt_read_buf_type (struct ckpt_ctx *ctx,int len,int type);extern int ckpt_read_consume (struct ckpt_ctx *ctx,int len,int type);Signature · 6 lines
1.6.2.5 _______________________________________________ Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org https://lists.linux-foundation.org/mailman/listinfo/containers
Re: [PATCH 2/5] Add a ckpt_read_string() function (v2)
In reply to a message not in the archiveQuoted 3 lines
checkpoint/restart.c int ckpt_read_consume(struct ckpt_ctx *ctx, int len, int type) · line 313
Quoted 3 lines
checkpoint/restart.c +3
-0+ *str =kzalloc (len +1 ,GFP_KERNEL ); +if (!*str) + ret = -ENOMEM ;
SH> would you like to return here as well?
Heh, I guess that would be best :)
(sigh)
Signature · 3 lines
Dan Smith IBM Linux Technology Center email: danms-r/[email protected]
Re: [PATCH 3/5] Add common socket helpers to unify the security hooks
In reply to Dan SmithQuoted 40 lines · Dan Smith
This moves the meat out of the
bind(),getsockname(), andgetpeername()syscalls into helper functions that performssecurity_socket_bind()and then thesock->ops->call(). This allows a unification of this behavior between the syscalls and the pending socket restart logic.Signed-off-by Dan Smith <[email protected]>--- include/net/sock.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ net/socket.c | 29 ++++++----------------------- 2 files changed, 54 insertions(+), 23 deletions(-)include/net/sock.h +20
-0@@ -1572,6 +1572,54 @@ extern void sock_enable_timestamp(struct sock *sk, int flag);extern int sock_get_timestamp (struct sock *,struct timeval __user *);extern int sock_get_timestampns (struct sock *,struct timespec __user *); +/* bind() helper shared between any callers needing to perform a bind on +* behalf of userspace (syscall and restart) with the security hooks. +*/ +static inline int sock_bind (struct socket *sock, +struct sockaddr *addr, +int addr_len) +{ +int err; + + err =security_socket_bind (sock, addr, addr_len); +if (err) +return err; +else +return sock->ops->bind (sock, addr, addr_len); +} + +/* getname() helper shared between any callers needing to perform a getname on +* behalf of userspace (syscall and restart) with the security hooks. +*/ +static inline int sock_getname(struct socket *sock,
include/net/sock.h extern void sock_enable_timestamp(struct sock *sk, int flag); · line 1604
Quoted 10 lines
include/net/sock.h +10
-0+struct sockaddr *addr, + int *addr_len) +{ + int err; + + err =security_socket_getsockname (sock); +if (err) +return err; +else +return sock->ops->getname (sock, addr, addr_len,0 );
My only concern here is whether 0 should be passed in by the caller? (Not sure, just wondering)
Otherwise this looks good, thanks.
Quoted 82 lines
include/net/sock.h +18
-0+} + +/* getpeer() helper shared between any callers needing to perform a getpeer on +* behalf of userspace (syscall and restart) with the security hooks. +*/ +static inline int sock_getpeer (struct socket *sock, +struct sockaddr *addr, +int *addr_len) +{ +int err; + + err =security_socket_getpeername (sock); +if (err) +return err; +else +return sock->ops->getname (sock, addr, addr_len,1 ); +} +/* * Enable debug/info messages */ net/socket.c +6
-23@@ -1414,15 +1414,10 @@ SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen) sock =sockfd_lookup_light (fd, &err, &fput_needed);if (sock) { err =move_addr_to_kernel (umyaddr, addrlen, (struct sockaddr *)&address); -if (err >=0 ) { - err =security_socket_bind (sock, - (struct sockaddr *)&address, - addrlen); -if (!err) - err = sock->ops->bind (sock, - (struct sockaddr *) - &address, addrlen); - } +if (err >=0 ) + err =sock_bind (sock, + (struct sockaddr *)&address, + addrlen);fput_light (sock->file, fput_needed); }return err;@@ -1610,11 +1605,7 @@ SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,if (!sock) goto out; - err =security_socket_getsockname (sock); -if (err) - goto out_put; - - err = sock->ops->getname (sock, (struct sockaddr *)&address, &len,0 ); + err =sock_getname (sock, (struct sockaddr *)&address, &len);if (err) goto out_put; err =move_addr_to_user ((struct sockaddr *)&address, len, usockaddr, usockaddr_len);@@ -1639,15 +1630,7 @@ SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr, sock =sockfd_lookup_light (fd, &err, &fput_needed);if (sock !=NULL ) { - err =security_socket_getpeername (sock); -if (err) { -fput_light (sock->file, fput_needed); -return err; - } - - err = - sock->ops->getname (sock, (struct sockaddr *)&address, &len, -1 ); + err =sock_getpeer (sock, (struct sockaddr *)&address, &len);if (!err) err =move_addr_to_user ((struct sockaddr *)&address, len, usockaddr, usockaddr_len);Signature · 6 lines
1.6.2.5 _______________________________________________ Containers mailing list [email protected] https://lists.linux-foundation.org/mailman/listinfo/containers
Serge E. Hallyn
Acked-byRe: [PATCH 4/5] Export fill_fname() as ckpt_fill_fname()
In reply to a message not in the archiveQuoted 1 line · Dan Smith
Signed-off-by Dan Smith <danms-r/[email protected]>
Quoted 56 lines
--- checkpoint/files.c | 8 ++++---- include/linux/checkpoint.h | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-)checkpoint/files.c +4
-4@@ -28,14 +28,14 @@*/ /** -* fill_fname - return pathname of a given file +* ckpt_fill_fname - return pathname of a given file * @path: path name * @root: relative root * @buf: buffer for pathname * @len: buffer length (in) and pathname length (out) */ -static char *fill_fname (struct path *path,struct path *root, -char *buf,int *len) +char *ckpt_fill_fname (struct path *path,struct path *root, +char *buf,int *len) {struct path tmp = *root;char *fname;@@ -79,7 +79,7 @@ int checkpoint_fname(struct ckpt_ctx *ctx, struct path *path, struct path *root)if (!buf)return -ENOMEM ; - fname =fill_fname (path, root, buf, &flen); + fname =ckpt_fill_fname (path, root, buf, &flen);if (!IS_ERR (fname)) ret =ckpt_write_obj_type (ctx, fname, flen,CKPT_HDR_FILE_NAME );include/linux/checkpoint.h +2
-0@@ -72,6 +72,8 @@ extern int ckpt_read_string(struct ckpt_ctx *ctx, char **str, int max);extern void *ckpt_read_obj_type (struct ckpt_ctx *ctx,int len,int type);extern void *ckpt_read_buf_type (struct ckpt_ctx *ctx,int len,int type);extern int ckpt_read_consume (struct ckpt_ctx *ctx,int len,int type); +extern char *ckpt_fill_fname (struct path *path,struct path *root, +char *buf,int *len);/* ckpt kflags */ #define ckpt_set_ctx_kflag (__ctx, __kflag) \Signature · 6 lines
1.6.2.5 _______________________________________________ Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org https://lists.linux-foundation.org/mailman/listinfo/containers
Re: [PATCH 3/5] Add common socket helpers to unify the security hooks
In reply to Serge E. HallynSH> My only concern here is whether 0 should be passed in by the SH> caller? (Not sure, just wondering)
I just figured I'd split up the 0/1 case into getname and getpeer, mirroring the libc calls. I don't care either way.
Signature · 3 lines
Dan Smith IBM Linux Technology Center email: [email protected]
Re: [PATCH 5/5] c/r: Add AF_UNIX support (v7)
In reply to Dan SmithQuoted 19 lines · Dan Smith
net/checkpoint.c new file +19
-0+static int sock_unix_checkpoint (struct ckpt_ctx *ctx, +struct socket *socket, +struct ckpt_hdr_socket *h) +{ +struct unix_sock *sk =unix_sk (socket->sk); +struct unix_sock *pr =unix_sk (sk->peer); +struct ckpt_hdr_socket_unix *un; +int new; +int ret = -ENOMEM ; + +if ((socket->sk->sk_state ==TCP_LISTEN ) && + !skb_queue_empty (&socket->sk->sk_receive_queue)) { +ckpt_write_err (ctx,"listening socket has unaccepted peers" ); +return -EBUSY ; + } + + un =ckpt_hdr_get_type (ctx,sizeof (*un),CKPT_HDR_SOCKET_UNIX ); +if (!un) + goto out;
...
Review
Quoted 2 lines
+ out:
(quoted diff) +1
-0+ ckpt_hdr_put(ctx, un);
This will cause a null deref trying to get ptr->len in ckpt_hdr_put().
-serge
Re: [PATCH 3/5] Add common socket helpers to unify the security hooks
In reply to Dan SmithQuoted 5 lines · Dan Smith
SH> My only concern here is whether 0 should be passed in by the SH> caller? (Not sure, just wondering)
I just figured I'd split up the 0/1 case into getname and getpeer, mirroring the libc calls. I don't care either way.
Oh, sounds sensible.
-serge
Re: [PATCH 5/5] c/r: Add AF_UNIX support (v7)
In reply to Dan SmithQuoting Dan Smith ([email protected]):
...
Quoted 8 lines
net/checkpoint.c new file +8
-0+static int sock_copy_buffers (struct sk_buff_head *from, +struct sk_buff_head *to, +uint32_t *total_bytes) +{ +int count =0 ; +struct sk_buff *skb; + + *total_bytes =0 ;
net/checkpoint.c line 53
Quoted 10 lines
net/checkpoint.c new file +10
-0+ + skb_queue_walk(from, skb) { +struct sk_buff *tmp; + + tmp =dev_alloc_skb (skb->len); +if (!tmp) +return -ENOMEM ; + +spin_lock (&from->lock); + tmp =skb_clone (skb,GFP_KERNEL );
Does this re-use of tmp make sense? (It only would if dev_alloc_skb() did a generic prealloc for any subsequent skb_clone() which i don't think is the case)
Also, do you need any kind of lock on the queue to make this walk safe, or do ensure below (sorry i'm slow and haven't gotten there) that all tasks with an open fd for either end of this sock are frozen?
Quoted 11 lines
net/checkpoint.c new file +11
-0+spin_unlock (&from->lock); + +skb_queue_tail (to, tmp); + count++; + *total_bytes += tmp->len; + } + +return count; +} + +static int __sock_write_buffers(struct ckpt_ctx *ctx,
net/checkpoint.c line 74
Quoted 10 lines
net/checkpoint.c new file +10
-0+ struct sk_buff_head *queue) +{ +struct sk_buff *skb; +int ret =0 ; + + skb_queue_walk(queue, skb) { +if (UNIXCB (skb).fp) { +ckpt_write_err (ctx,"fd-passing is not supported" ); +return -EBUSY ; + }
what about UNIXCB(skb).creds and .secid?
Quoted 8 lines
net/checkpoint.c new file +8
-0+ ret =ckpt_write_obj_type (ctx, skb->data, skb->len, +CKPT_HDR_SOCKET_BUFFER ); +if (ret) +return ret; + } + +return 0 ; +}
net/checkpoint.c line 93
Quoted 10 lines
net/checkpoint.c new file +10
-0+ +static int sock_write_buffers (struct ckpt_ctx *ctx,struct sk_buff_head *queue) +{ +struct ckpt_hdr_socket_queue *h; +struct sk_buff_head tmpq; +int ret = -ENOMEM ; + + h =ckpt_hdr_get_type (ctx,sizeof (*h),CKPT_HDR_SOCKET_QUEUE ); +if (!h) + goto out;
Again, you can't ckpt_hdr_put with a NULL h.
Quoted 20 lines
net/checkpoint.c new file +20
-0+skb_queue_head_init (&tmpq); + + ret =sock_copy_buffers (queue, &tmpq, &h->total_bytes); +if (ret <0 ) + goto out; + + h->skb_count = ret; + ret =ckpt_write_obj (ctx, (struct ckpt_hdr *) h); +if (!ret) + ret =__sock_write_buffers (ctx, &tmpq); + + out: +ckpt_hdr_put (ctx, h); +__skb_queue_purge (&tmpq); + +return ret; +} + +static int sock_unix_write_cwd(struct ckpt_ctx *ctx, + struct sock *sock,
net/checkpoint.c line 124
Quoted 10 lines
net/checkpoint.c new file +10
-0+ const char *sockpath) +{ +struct path path; +char *buf; +char *fqpath; +int offset; +int len =PATH_MAX ; +int ret = -ENOENT ; + + buf =kmalloc (PATH_MAX ,GFP_KERNEL );
sending len to kmalloc instead of PATH_MAX might be more future-proof...
Quoted 16 lines
net/checkpoint.c new file +16
-0+if (!buf) +return -ENOMEM ; + + path.dentry =unix_sk (sock)->dentry; + path.mnt =unix_sk (sock)->mnt; + + fqpath =ckpt_fill_fname (&path, &ctx->fs_mnt, buf, &len); +if (IS_ERR (fqpath)) { + ret =PTR_ERR (fqpath); + goto out; + } + + offset =strlen (fqpath) -strlen (sockpath); +if (offset <=0 ) { + ret = -EINVAL ; + goto out;
net/checkpoint.c line 150
Quoted 10 lines
net/checkpoint.c new file +10
-0+ } + + fqpath[offset] ='\0' ; + +ckpt_debug ("writing socket directory: %s\n" , fqpath); + ret =ckpt_write_string (ctx, fqpath,strlen (fqpath)); + out: +kfree (buf); +return ret; +}
looks good.
net/checkpoint.c line 158
Quoted 7 lines
net/checkpoint.c new file +7
-0+static int sock_getnames (struct ckpt_ctx *ctx, +struct socket *socket, +struct sockaddr *loc,unsigned *loc_len, +struct sockaddr *rem,unsigned *rem_len) +{ +if (sock_getname (socket, loc, loc_len)) { +ckpt_write_err (ctx,"Unable to getname of local" );
maybe log the errno?
Quoted 160 lines
net/checkpoint.c new file +159
-0+return -EINVAL ; + } + +if (sock_getpeer (socket, rem, rem_len)) { +if ((socket->sk->sk_type !=SOCK_DGRAM ) && + (socket->sk->sk_state ==TCP_ESTABLISHED )) { +ckpt_write_err (ctx,"Unable to getname of remote" ); +return -EINVAL ; + } + *rem_len =0 ; + } + +return 0 ; +} + +static int sock_unix_checkpoint (struct ckpt_ctx *ctx, +struct socket *socket, +struct ckpt_hdr_socket *h) +{ +struct unix_sock *sk =unix_sk (socket->sk); +struct unix_sock *pr =unix_sk (sk->peer); +struct ckpt_hdr_socket_unix *un; +int new; +int ret = -ENOMEM ; + +if ((socket->sk->sk_state ==TCP_LISTEN ) && + !skb_queue_empty (&socket->sk->sk_receive_queue)) { +ckpt_write_err (ctx,"listening socket has unaccepted peers" ); +return -EBUSY ; + } + + un =ckpt_hdr_get_type (ctx,sizeof (*un),CKPT_HDR_SOCKET_UNIX ); +if (!un) + goto out; + + ret =sock_getnames (ctx, socket, + (struct sockaddr *)&un->laddr, &un->laddr_len, + (struct sockaddr *)&un->raddr, &un->raddr_len); +if (ret) + goto out; + +if (sk->dentry && (sk->dentry->d_inode->i_nlink >0 )) + un->flags |=CKPT_UNIX_LINKED ; + + un->this =ckpt_obj_lookup_add (ctx, sk,CKPT_OBJ_SOCK , &new); +if (un->this <0 ) + goto out; + +if (sk->peer) + un->peer =ckpt_obj_lookup_add (ctx, pr,CKPT_OBJ_SOCK , &new); +else + un->peer =0 ; + +if (un->peer <0 ) { + ret = un->peer; + goto out; + } + + ret =ckpt_write_obj (ctx, (struct ckpt_hdr *) h); +if (ret <0 ) + goto out; + + ret =ckpt_write_obj (ctx, (struct ckpt_hdr *) un); +if (ret <0 ) + goto out; + +if (sock_unix_need_cwd (&un->laddr, un->laddr_len)) + ret =sock_unix_write_cwd (ctx, socket->sk, un->laddr.sun_path); + out: +ckpt_hdr_put (ctx, un); + +return ret; +} + +static int sock_cptrst_verify (struct ckpt_hdr_socket *h) +{ +uint8_t userlocks_mask =SOCK_SNDBUF_LOCK |SOCK_RCVBUF_LOCK | +SOCK_BINDADDR_LOCK |SOCK_BINDPORT_LOCK ; + +if (h->sock.shutdown & ~SHUTDOWN_MASK ) +return -EINVAL ; +if (h->sock.userlocks & ~userlocks_mask) +return -EINVAL ; +if (!ckpt_validate_errno (h->sock.err)) +return -EINVAL ; + +/* None of our supported types use this flag */ +if (h->sock.flags &SOCK_DESTROY ) +return -EINVAL ; + +return 0 ; +} + +static int sock_cptrst_opt (int op,struct socket *socket, +int optname,char *opt,int len) +{ +mm_segment_t fs; +int ret; + + fs =get_fs (); +set_fs (KERNEL_DS ); + +if (op ==CKPT_CPT ) + ret =sock_getsockopt (socket,SOL_SOCKET , optname, opt, &len); +else + ret =sock_setsockopt (socket,SOL_SOCKET , optname, opt, len); + +set_fs (fs); + +return ret; +} + +#define CKPT_COPY_SOPT (op, sock, name, opt) \ + sock_cptrst_opt(op, sock->sk_socket, name, (char *)opt, sizeof(*opt)) + +static int sock_cptrst_bufopts (int op,struct sock *sock, +struct ckpt_hdr_socket *h) + +{ +if (CKPT_COPY_SOPT (op, sock,SO_RCVBUF , &h->sock.rcvbuf)) +if ((op ==CKPT_RST ) && +CKPT_COPY_SOPT (op, sock,SO_RCVBUFFORCE , &h->sock.rcvbuf)) { +ckpt_debug ("Failed to set SO_RCVBUF" ); +return -EINVAL ; + } + +if (CKPT_COPY_SOPT (op, sock,SO_SNDBUF , &h->sock.sndbuf)) +if ((op ==CKPT_RST ) && +CKPT_COPY_SOPT (op, sock,SO_SNDBUFFORCE , &h->sock.sndbuf)) { +ckpt_debug ("Failed to set SO_SNDBUF" ); +return -EINVAL ; + } + +/* It's silly that we have to fight ourselves here, but +* sock_setsockopt() doubles the initial value, so divide here +* to store the user's value and avoid doubling on restart +*/ +if ((op ==CKPT_CPT ) && (h->sock.rcvbuf !=SOCK_MIN_RCVBUF )) + h->sock.rcvbuf >>=1 ; + +if ((op ==CKPT_CPT ) && (h->sock.sndbuf !=SOCK_MIN_SNDBUF )) + h->sock.sndbuf >>=1 ; + +return 0 ; +} + +static int sock_cptrst (struct ckpt_ctx *ctx, +struct sock *sock, +struct ckpt_hdr_socket *h, +int op) +{ +if (sock->sk_socket) { +CKPT_COPY (op, h->socket.flags, sock->sk_socket->flags); +CKPT_COPY (op, h->socket.state, sock->sk_socket->state); + } + +CKPT_COPY (op, h->sock_common.bound_dev_if, sock->sk_bound_dev_if); +CKPT_COPY (op, h->sock_common.family, sock->sk_family); +
net/checkpoint.c line 327
Quoted 10 lines
net/checkpoint.c new file +10
-0+CKPT_COPY (op, h->sock.shutdown, sock->sk_shutdown); +CKPT_COPY (op, h->sock.userlocks, sock->sk_userlocks); +CKPT_COPY (op, h->sock.no_check, sock->sk_no_check); +CKPT_COPY (op, h->sock.protocol, sock->sk_protocol); +CKPT_COPY (op, h->sock.err, sock->sk_err); +CKPT_COPY (op, h->sock.err_soft, sock->sk_err_soft); +CKPT_COPY (op, h->sock.backlog, sock->sk_max_ack_backlog); +CKPT_COPY (op, h->sock.flags, sock->sk_flags); +CKPT_COPY (op, h->sock.type, sock->sk_type); +CKPT_COPY (op, h->sock.state, sock->sk_state);
It looks like the above provides a way around needing CAP_NET_ADMIN to set SOCK_DBG in sock->sk_flags? You can probably fix that by masking it out here, and if a flag in the checkpoint image says it was on originally, then set it below through setsockopt.
Sanity checking on sk_type, sk_state, backlog etc should probably also be added.
I do like how you re-use setsockopt below to maintain security and sanity checks on those fields.
Quoted 87 lines
net/checkpoint.c new file +87
-0+ +if (sock_cptrst_bufopts (op, sock, h)) +return -EINVAL ; + +if (CKPT_COPY_SOPT (op, sock,SO_REUSEADDR , &h->sock_common.reuse)) { +ckpt_debug ("Failed to set SO_REUSEADDR" ); +return -EINVAL ; + } + +if (CKPT_COPY_SOPT (op, sock,SO_PRIORITY , &h->sock.priority)) { +ckpt_debug ("Failed to set SO_PRIORITY" ); +return -EINVAL ; + } + +if (CKPT_COPY_SOPT (op, sock,SO_RCVLOWAT , &h->sock.rcvlowat)) { +ckpt_debug ("Failed to set SO_RCVLOWAT" ); +return -EINVAL ; + } + +if (CKPT_COPY_SOPT (op, sock,SO_LINGER , &h->sock.linger)) { +ckpt_debug ("Failed to set SO_LINGER" ); +return -EINVAL ; + } + +if (CKPT_COPY_SOPT (op, sock,SO_SNDTIMEO , &h->sock.sndtimeo)) { +ckpt_debug ("Failed to set SO_SNDTIMEO" ); +return -EINVAL ; + } + +if (CKPT_COPY_SOPT (op, sock,SO_RCVTIMEO , &h->sock.rcvtimeo)) { +ckpt_debug ("Failed to set SO_RCVTIMEO" ); +return -EINVAL ; + } + +if ((h->socket.state ==SS_CONNECTED ) && + (h->sock.state !=TCP_ESTABLISHED )) { +ckpt_debug ("socket/sock in inconsistent state: %i/%i" , + h->socket.state, h->sock.state); +return -EINVAL ; + }else if ((h->sock.state <TCP_ESTABLISHED ) || + (h->sock.state >=TCP_MAX_STATES )) { +ckpt_debug ("sock in invalid state: %i" , h->sock.state); +return -EINVAL ; + }else if ((h->socket.state <SS_FREE ) || + (h->socket.state >SS_DISCONNECTING )) { +ckpt_debug ("socket in invalid state: %i" , + h->socket.state); +return -EINVAL ; + } + +if (op ==CKPT_CPT ) +return sock_cptrst_verify (h); +else +return 0 ; +} + +int do_sock_file_checkpoint (struct ckpt_ctx *ctx,struct file *file) +{ +struct socket *socket = file->private_data; +struct sock *sock = socket->sk; +struct ckpt_hdr_socket *h; +int ret =0 ; + + h =ckpt_hdr_get_type (ctx,sizeof (*h),CKPT_HDR_SOCKET ); +if (!h) +return -ENOMEM ; + + ret =sock_cptrst (ctx, sock, h,CKPT_CPT ); +if (ret) + goto out; + +if (sock->sk_family ==AF_UNIX ) { + ret =sock_unix_checkpoint (ctx, socket, h); +if (ret) + goto out; + }else { +ckpt_write_err (ctx,"unsupported socket family %i" , + sock->sk_family); + ret = -ENOSYS ; + goto out; + } + +if (sock->sk_state !=TCP_LISTEN ) { + ret =sock_write_buffers (ctx, &sock->sk_receive_queue); +if (ret) + goto out; +
net/checkpoint.c line 424
Quoted 10 lines
net/checkpoint.c new file +10
-0+ ret =sock_write_buffers (ctx, &sock->sk_write_queue); +if (ret) + goto out; + } + out: +ckpt_hdr_put (ctx, h); + +return ret; +} +
(will go on from here later :)
-serge
Re: [PATCH 5/5] c/r: Add AF_UNIX support (v7)
SH> Does this re-use of tmp make sense? (It only would if SH> dev_alloc_skb() did a generic prealloc for any subsequent SH> skb_clone() which i don't think is the case)
No, this is cruft.
SH> Also, do you need any kind of lock on the queue to make this walk SH> safe, or do ensure below (sorry i'm slow and haven't gotten there) SH> that all tasks with an open fd for either end of this sock are SH> frozen?
Hmm, it seems that holding the lock while processing the queue isn't really the way to go. Perhaps comparing the pid of the other end of the socket against the list in the context is best?
SH> what about UNIXCB(skb).creds and .secid?
Yep, okay.
SH> It looks like the above provides a way around needing SH> CAP_NET_ADMIN to set SOCK_DBG in sock->sk_flags? You can probably SH> fix that by masking it out here, and if a flag in the checkpoint SH> image says it was on originally, then set it below through SH> setsockopt.
Yep, okay.
SH> Sanity checking on sk_type, sk_state, backlog etc should probably SH> also be added.
I check type and state on restart globally and per-protocol. Backlog could use it though too, yeah.
Thanks!
Signature · 3 lines
Dan Smith IBM Linux Technology Center email: [email protected]
Re: [PATCH 5/5] c/r: Add AF_UNIX support (v7)
Quoted 6 lines · Dan Smith
SH> Does this re-use of tmp make sense? (It only would if SH>
dev_alloc_skb()did a generic prealloc for any subsequent SH>skb_clone()which i don't think is the case)No, this is cruft.
Quoted 8 lines
SH> Also, do you need any kind of lock on the queue to make this walk SH> safe, or do ensure below (sorry i'm slow and haven't gotten there) SH> that all tasks with an open fd for either end of this sock are SH> frozen?
Hmm, it seems that holding the lock while processing the queue isn't really the way to go. Perhaps comparing the pid of the other end of the socket against the list in the context is best?
I don't understand. Which pid?
Re: [PATCH 5/5] c/r: Add AF_UNIX support (v7)
SH> I don't understand. Which pid?
I meant the socket's peercred, which includes the pid of the process that opened it, but that clearly won't work... :)
Signature · 3 lines
Dan Smith IBM Linux Technology Center email: [email protected]
Re: [PATCH 5/5] c/r: Add AF_UNIX support (v7)
In reply to Dan SmithQuoted 24 lines · Dan Smith
net/checkpoint.c new file +24
-0+static int sock_read_buffer_sendmsg (struct ckpt_ctx *ctx,struct sock *sock) +{ +struct msghdr msg; +struct kvec kvec; +int ret =0 ; +int len; + +memset (&msg,0 ,sizeof (msg)); + + len =_ckpt_read_obj_type (ctx,NULL ,0 ,CKPT_HDR_SOCKET_BUFFER ); +if (len <0 ) +return len; + +if (len >SKB_MAX_ALLOC ) { +ckpt_debug ("Socket buffer too big (%i > %lu)" , + len,SKB_MAX_ALLOC ); +return -ENOSPC ; + } + + kvec.iov_len = len; + kvec.iov_base =kmalloc (len,GFP_KERNEL ); +if (!kvec.iov_base) +return -ENOMEM ; +
net/checkpoint.c line 458
Quoted 10 lines
net/checkpoint.c new file +10
-0+ ret =ckpt_kread (ctx, kvec.iov_base, len); +if (ret <0 ) + goto out; + + ret =kernel_sendmsg (sock->sk_socket, &msg, &kvec,1 , len); +ckpt_debug ("kernel_sendmsg(%i): %i\n" , len, ret); +if ((ret >0 ) && (ret != len)) + ret = -ENOMEM ; + out: +if (ret)
why only free iov_base if ret!=0?
Quoted 106 lines
net/checkpoint.c new file +106
-0+kfree (kvec.iov_base); + +return ret; +} + +static struct ckpt_hdr_socket_queue *sock_read_buffer_hdr (struct ckpt_ctx *ctx, +uint32_t *bufsize) +{ +struct ckpt_hdr_socket_queue *h; +int err =0 ; + + h =ckpt_read_obj_type (ctx,sizeof (*h),CKPT_HDR_SOCKET_QUEUE ); +if (IS_ERR (h)) +return h; + +if (!bufsize) { +if (h->total_bytes !=0 ) { +ckpt_debug ("Expected empty buffer, got %u\n" , + h->total_bytes); + err = -EINVAL ; + } + }else if (h->total_bytes > *bufsize) { +/* NB: We let CAP_NET_ADMIN override the system buffer limit +* as setsockopt() does +*/ +if (capable (CAP_NET_ADMIN )) + *bufsize = h->total_bytes; +else { +ckpt_debug ("Buffer total %u exceeds limit %u\n" , + h->total_bytes, *bufsize); + err = -EINVAL ; + } + } + +if (err) { +ckpt_hdr_put (ctx, h); +return ERR_PTR (err); + }else +return h; +} + +static int sock_unix_read_buffers (struct ckpt_ctx *ctx, +struct sock *sock, +uint32_t *bufsize) +{ +uint8_t sock_shutdown; +struct ckpt_hdr_socket_queue *h; +int ret =0 ; +int i; + + h =sock_read_buffer_hdr (ctx, bufsize); +if (IS_ERR (h)) +return PTR_ERR (h); + +/* If peer is shutdown, unshutdown it for this process */ + sock_shutdown = sock->sk_shutdown; + sock->sk_shutdown &= ~SHUTDOWN_MASK ; + +for (i =0 ; i < h->skb_count; i++) { + ret =sock_read_buffer_sendmsg (ctx, sock); +ckpt_debug ("read_buffer_sendmsg(%i): %i\n" , i, ret); +if (ret <0 ) +break ; + +if (ret > h->total_bytes) { +ckpt_debug ("Buffers exceeded claim" ); + ret = -EINVAL ; +break ; + } + + h->total_bytes -= ret; + ret =0 ; + } + + sock->sk_shutdown = sock_shutdown; +ckpt_hdr_put (ctx, h); + +return ret; +} + +static struct unix_address *sock_unix_makeaddr (struct sockaddr_un *sun_addr, +unsigned len) +{ +struct unix_address *addr; + +if (len >sizeof (struct sockaddr_un )) +return ERR_PTR (-EINVAL ); + + addr =kmalloc (sizeof (*addr) + len,GFP_KERNEL ); +if (!addr) +return ERR_PTR (-ENOMEM ); + +memcpy (addr->name, sun_addr, len); + addr->len = len; +atomic_set (&addr->refcnt,1 ); + +return addr; +} + +static int sock_unix_join (struct ckpt_ctx *ctx, +struct sock *a, +struct sock *b, +struct ckpt_hdr_socket_unix *un) +{ +struct unix_address *addr =NULL ; +
net/checkpoint.c line 574
Quoted 10 lines
net/checkpoint.c new file +10
-0+/* FIXME: Do we need to call some security hooks here? */ + +sock_hold (a); +sock_hold (b); + +unix_sk (a)->peer = b; +unix_sk (b)->peer = a; + + a->sk_peercred.pid =task_tgid_vnr (current); + a->sk_peercred.uid = ctx->realcred->uid;
I don't know how much it matters, but of course root could be restarting a set of tasks owned by several non-root uids, and the peercred.uid's might need to be something other than ctx->realcred->uid. Or not?
net/checkpoint.c line 580
Quoted 6 lines
net/checkpoint.c new file +6
-0+ a->sk_peercred.gid = ctx->realcred->gid; + + b->sk_peercred.pid = a->sk_peercred.pid; + b->sk_peercred.uid = a->sk_peercred.uid; + b->sk_peercred.gid = a->sk_peercred.gid; +
-serge
Re: [PATCH 5/5] c/r: Add AF_UNIX support (v7)
SH> why only free iov_base if ret!=0?
Because I was diagnosing a crash that only seemed to happen when I free()'d the buffer after it was used by sendmsg() and I forgot to remove this :(
Quoted 2 lines
net/checkpoint.c line 574
Quoted 2 lines
net/checkpoint.c new file +2
-0+ a->sk_peercred.pid =task_tgid_vnr (current); + a->sk_peercred.uid = ctx->realcred->uid;
SH> I don't know how much it matters, but of course root could be SH> restarting a set of tasks owned by several non-root uids, and the SH> peercred.uid's might need to be something other than ctx-> realcred->uid. Or not?
Oh, so you're suggesting I use ctx->ecred instead? I didn't actually notice the double declaration in the ckpt_ctx, but I guess that would be better.
Signature · 3 lines
Dan Smith IBM Linux Technology Center email: [email protected]
Re: [PATCH 5/5] c/r: Add AF_UNIX support (v7)
Quoted 14 lines · Dan Smith
SH> why only free
iov_baseif ret!=0?Because I was diagnosing a crash that only seemed to happen when I free()'d the buffer after it was used by
sendmsg()and I forgot to remove this :(Quoted 2 lines
net/checkpoint.c line 574
Quoted 2 lines
net/checkpoint.c new file +2
-0+ a->sk_peercred.pid =task_tgid_vnr (current); + a->sk_peercred.uid = ctx->realcred->uid;SH> I don't know how much it matters, but of course root could be SH> restarting a set of tasks owned by several non-root uids, and the SH> peercred.uid's might need to be something other than ctx->
realcred->uid. Or not?
Quoted 3 lines
Oh, so you're suggesting I use
ctx->ecredinstead? I didn't actually notice the double declaration in theckpt_ctx, but I guess that would be better.
No, I'm suggesting that the checkpointed application might have had tasks owned by uids 0, 3, 55, and 1001, and a.peercred.uid might have been 1001, right? current, ctx->realcred->uid, and ctx->ecred might all be different uids. I think you just need to checkpoint the uid (eventually an objref to a checkpointed user struct so we can also catch the user namespace).
-serge