]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Only accept netlink messages from the kernel.
authorRoy Marples <roy@marples.name>
Fri, 24 Aug 2012 19:05:26 +0000 (19:05 +0000)
committerRoy Marples <roy@marples.name>
Fri, 24 Aug 2012 19:05:26 +0000 (19:05 +0000)
Thanks to Mikhail Efremov.

if-linux.c

index d9db786d8bb03f05137162a4b2fd319f373aa1b8..f39507a4095d117eb6c7c2005a1ab3231c2ec7f2 100644 (file)
@@ -153,6 +153,8 @@ get_netlink(int fd, int flags,
        char *buf = NULL, *nbuf;
        ssize_t buflen = 0, bytes;
        struct nlmsghdr *nlm;
+       struct sockaddr_nl nladdr;
+       socklen_t nladdr_len = sizeof(nladdr);
        int r = -1;
 
        for (;;) {
@@ -181,7 +183,8 @@ get_netlink(int fd, int flags,
                                goto eexit;
                        buf = nbuf;
                }
-               bytes = recv(fd, buf, buflen, flags);
+               bytes = recvfrom(fd, buf, buflen, flags,
+                   (struct sockaddr *)&nladdr, &nladdr_len);
                if (bytes == -1) {
                        if (errno == EAGAIN) {
                                r = 0;
@@ -191,6 +194,16 @@ get_netlink(int fd, int flags,
                                continue;
                        goto eexit;
                }
+
+               /* Check sender */
+               if (nladdr_len != sizeof(nladdr)) {
+                       errno = EINVAL;
+                       goto eexit;
+               }
+               /* Ignore message if it is not from kernel */
+               if (nladdr.nl_pid != 0)
+                       continue;
+
                for (nlm = (struct nlmsghdr *)(void *)buf;
                     NLMSG_OK(nlm, (size_t)bytes);
                     nlm = NLMSG_NEXT(nlm, bytes))