From 420e4623fd1ebf2a14e2977d82fd477f1e913460 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=D0=9A=D0=BE=D1=80=D0=B5=D0=BD=D0=B1=D0=B5=D1=80=D0=B3=20?= =?utf8?q?=D0=9C=D0=B0=D1=80=D0=BA=20=28=D0=B4=D0=BE=D0=BC=D0=B0=29?= Date: Fri, 19 Oct 2012 23:23:10 +0600 Subject: [PATCH] nl_recv(): work with credentials only if "creds" given and NL_SOCK_PASSCRED set --- lib/nl.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/nl.c b/lib/nl.c index 284d23c..9858307 100644 --- a/lib/nl.c +++ b/lib/nl.c @@ -407,7 +407,7 @@ errout: * @arg sk Netlink socket. * @arg nla Destination pointer for peer's netlink address. (required) * @arg buf Destination pointer for message content. (required) - * @arg creds Destination pointer for credentials. + * @arg creds Destination pointer for credentials. (optional) * * Receives a netlink message, allocates a buffer in \c *buf and * stores the message content. The peer's netlink address is stored @@ -451,7 +451,7 @@ int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla, goto abort; } - if (sk->s_flags & NL_SOCK_PASSCRED) { + if (creds && (sk->s_flags & NL_SOCK_PASSCRED)) { msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred)); msg.msg_control = malloc(msg.msg_controllen); if (!msg.msg_control) { @@ -519,17 +519,20 @@ retry: goto abort; } - for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { - if (cmsg->cmsg_level == SOL_SOCKET && - cmsg->cmsg_type == SCM_CREDENTIALS) { - if (creds) { - tmpcreds = malloc(sizeof(*tmpcreds)); - if (!tmpcreds) { - retval = -NLE_NOMEM; - goto abort; - } - memcpy(tmpcreds, CMSG_DATA(cmsg), sizeof(*tmpcreds)); + if (creds && (sk->s_flags & NL_SOCK_PASSCRED)) { + struct cmsghdr *cmsg; + + for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { + if (cmsg->cmsg_level != SOL_SOCKET) + continue; + if (cmsg->cmsg_type != SCM_CREDENTIALS) + continue; + tmpcreds = malloc(sizeof(*tmpcreds)); + if (!tmpcreds) { + retval = -NLE_NOMEM; + goto abort; } + memcpy(tmpcreds, CMSG_DATA(cmsg), sizeof(*tmpcreds)); break; } } -- 2.40.0