From: Christian Brauner Date: Tue, 23 Feb 2021 07:45:32 +0000 (+0100) Subject: utils: add copy_struct_from_client() X-Git-Tag: lxc-5.0.0~273^2~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d329cda71b8972fdac2a4fc70403fc81602c1e53;p=thirdparty%2Flxc.git utils: add copy_struct_from_client() Which is our variant of copy_struct_from_user() that Aleksa and I added to the kernel. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/utils.h b/src/lxc/utils.h index dd51afdc4..f2b50e87b 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -245,4 +245,22 @@ __hidden extern int safe_mount_beneath_at(int beneat_fd, const char *src, const const char *fstype, unsigned int flags, const void *data); __hidden __lxc_unused int print_r(int fd, const char *path); +static inline int copy_struct_from_client(__u32 server_size, void *dst, + __u32 client_size, const void *src) +{ + __u32 size = min(server_size, client_size); + __u32 rest = min(server_size, client_size) - size; + + /* Deal with trailing bytes. */ + if (client_size < server_size) { + memset(dst + size, 0, rest); + } else if (client_size > server_size) { + /* TODO: Actually come up with a nice way to test for 0. */ + return 0; + } + + memcpy(dst, src, size); + return 0; +} + #endif /* __LXC_UTILS_H */