Skip to content

Commit 3f551f9

Browse files
ebiedermdavem330
authored andcommitted
sock: Introduce cred_to_ucred
To keep the coming code clear and to allow both the sock code and the scm code to share the logic introduce a fuction to translate from struct cred to struct ucred. Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Acked-by: Pavel Emelyanov <xemul@openvz.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 5c1469d commit 3f551f9

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

include/linux/socket.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ struct __kernel_sockaddr_storage {
2424
#include <linux/types.h> /* pid_t */
2525
#include <linux/compiler.h> /* __user */
2626

27+
struct pid;
28+
struct cred;
29+
2730
#define __sockaddr_check_size(size) \
2831
BUILD_BUG_ON(((size) > sizeof(struct __kernel_sockaddr_storage)))
2932

@@ -309,6 +312,8 @@ struct ucred {
309312
#define IPX_TYPE 1
310313

311314
#ifdef __KERNEL__
315+
extern void cred_to_ucred(struct pid *pid, const struct cred *cred, struct ucred *ucred);
316+
312317
extern int memcpy_fromiovec(unsigned char *kdata, struct iovec *iov, int len);
313318
extern int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
314319
int offset, int len);

net/core/sock.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
#include <linux/tcp.h>
111111
#include <linux/init.h>
112112
#include <linux/highmem.h>
113+
#include <linux/user_namespace.h>
113114

114115
#include <asm/uaccess.h>
115116
#include <asm/system.h>
@@ -749,6 +750,19 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
749750
EXPORT_SYMBOL(sock_setsockopt);
750751

751752

753+
void cred_to_ucred(struct pid *pid, const struct cred *cred,
754+
struct ucred *ucred)
755+
{
756+
ucred->pid = pid_vnr(pid);
757+
ucred->uid = ucred->gid = -1;
758+
if (cred) {
759+
struct user_namespace *current_ns = current_user_ns();
760+
761+
ucred->uid = user_ns_map_uid(current_ns, cred, cred->euid);
762+
ucred->gid = user_ns_map_gid(current_ns, cred, cred->egid);
763+
}
764+
}
765+
752766
int sock_getsockopt(struct socket *sock, int level, int optname,
753767
char __user *optval, int __user *optlen)
754768
{

0 commit comments

Comments
 (0)