Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/net/net Add a test demonstrating thst LOCAL_PEEREID is...



details:   https://anonhg.NetBSD.org/src/rev/3a81ea7f3f35
branches:  trunk
changeset: 829916:3a81ea7f3f35
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Feb 17 16:34:18 2018 +0000

description:
Add a test demonstrating thst LOCAL_PEEREID is busted.

diffstat:

 tests/net/net/t_unix.c |  196 +++++++++++++++++++++++++++++++------------------
 1 files changed, 124 insertions(+), 72 deletions(-)

diffs (292 lines):

diff -r 05b06cacff44 -r 3a81ea7f3f35 tests/net/net/t_unix.c
--- a/tests/net/net/t_unix.c    Sat Feb 17 15:22:22 2018 +0000
+++ b/tests/net/net/t_unix.c    Sat Feb 17 16:34:18 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_unix.c,v 1.15 2018/02/16 22:17:17 christos Exp $     */
+/*     $NetBSD: t_unix.c,v 1.16 2018/02/17 16:34:18 christos Exp $     */
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #ifdef __RCSID
-__RCSID("$Id: t_unix.c,v 1.15 2018/02/16 22:17:17 christos Exp $");
+__RCSID("$Id: t_unix.c,v 1.16 2018/02/17 16:34:18 christos Exp $");
 #else
 #define getprogname() argv[0]
 #endif
@@ -51,6 +51,7 @@
 #include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#include <sys/wait.h>
 #include <stdio.h>
 #include <err.h>
 #include <errno.h>
@@ -126,18 +127,19 @@
 }
 
 static int
-test(bool closeit, size_t len)
+test(bool forkit, bool closeit, size_t len)
 {
        size_t slen;
-       socklen_t sl;
+       socklen_t sl, crl;
        int srvr = -1, clnt = -1, acpt = -1;
-       uid_t euid;
-       gid_t egid;
+       pid_t srvrpid, clntpid;
        struct sockaddr_un *sock_addr = NULL, *sun = NULL;
        socklen_t sock_addrlen;
        socklen_t peer_addrlen;
        struct sockaddr_un peer_addr;
+       struct unpcbid cred;
 
+       srvrpid = clntpid = getpid();
        srvr = socket(AF_UNIX, SOCK_STREAM, 0);
        if (srvr == -1)
                FAIL("socket(srvrer)");
@@ -172,70 +174,99 @@
        if (listen(srvr, SOMAXCONN) == -1)
                FAIL("listen");
 
-       clnt = socket(AF_UNIX, SOCK_STREAM, 0);
-       if (clnt == -1)
-               FAIL("socket(client)");
+       if (forkit) {
+               switch (clntpid = fork()) {
+               case 0: /* child */
+                       srvrpid = getppid();
+                       clntpid = getpid();
+                       break;
+               case -1:
+                       FAIL("fork");
+               default:
+                       break;
+               }
+       }
 
-       if (connect(clnt, (const struct sockaddr *)sun, sl) == -1)
-               FAIL("connect");
-
-       if (getpeereid(clnt, &euid, &egid) == -1)
-               FAIL("getpeereid(clnt)");
-       CHECK_EQUAL(euid, geteuid(), "client");
-       CHECK_EQUAL(egid, getegid(), "client");
-
+       if (clntpid == getpid()) {
+               clnt = socket(AF_UNIX, SOCK_STREAM, 0);
+               if (clnt == -1)
+                       FAIL("socket(client)");
 
-       acpt = acc(srvr);
+               if (connect(clnt, (const struct sockaddr *)sun, sl) == -1)
+                       FAIL("connect");
 
-       peer_addrlen = sizeof(peer_addr);
-       memset(&peer_addr, 0, sizeof(peer_addr));
-       if (getpeername(acpt, (struct sockaddr *)&peer_addr,
-           &peer_addrlen) == -1)
-               FAIL("getpeername");
-       print("peer", &peer_addr, peer_addrlen);
+               crl = sizeof(cred);
+               if (getsockopt(clnt, 0, LOCAL_PEEREID, &cred, &crl) == -1)
+                       FAIL("getsockopt()");
+               CHECK_EQUAL(cred.unp_euid, geteuid(), "client");
+               CHECK_EQUAL(cred.unp_egid, getegid(), "client");
+               CHECK_EQUAL(cred.unp_pid, srvrpid, "client");
+       }
 
-       if (closeit) {
-               if (close(clnt) == -1)
-                       FAIL("close");
-               clnt = -1;
+       if (srvrpid == getpid()) {
+               acpt = acc(srvr);
+
+               peer_addrlen = sizeof(peer_addr);
+               memset(&peer_addr, 0, sizeof(peer_addr));
+               if (getpeername(acpt, (struct sockaddr *)&peer_addr,
+                   &peer_addrlen) == -1)
+                       FAIL("getpeername");
+               print("peer", &peer_addr, peer_addrlen);
        }
 
-       /* This is not symmetric? It is supposed to work! */
-       if (getpeereid(acpt, &euid, &egid) == -1)
-               FAIL("getpeereid(srvr)");
-       CHECK_EQUAL(euid, geteuid(), "server");
-       CHECK_EQUAL(egid, getegid(), "server");
+       if (clntpid == getpid()) {
+               if (closeit) {
+                       if (close(clnt) == -1)
+                               FAIL("close");
+                       clnt = -1;
+               }
+       }
 
-       if ((sock_addr = calloc(1, slen)) == NULL)
-               FAIL("calloc");
-       sock_addrlen = slen;
-       if (getsockname(srvr, (struct sockaddr *)sock_addr, &sock_addrlen)
-           == -1)
-               FAIL("getsockname");
-       print("sock", sock_addr, sock_addrlen);
+       if (srvrpid == getpid()) {
+               crl = sizeof(cred);
+               if (getsockopt(acpt, 0, LOCAL_PEEREID, &cred, &crl) == -1)
+                       FAIL("getsockopt()");
+               CHECK_EQUAL(cred.unp_euid, geteuid(), "server");
+               CHECK_EQUAL(cred.unp_egid, getegid(), "server");
+               CHECK_EQUAL(cred.unp_pid, clntpid, "client");
 
-       if (sock_addr->sun_family != AF_UNIX)
-               FAIL("sock_addr->sun_family %d != AF_UNIX",
-                   sock_addr->sun_family);
+               if ((sock_addr = calloc(1, slen)) == NULL)
+                       FAIL("calloc");
+               sock_addrlen = slen;
+               if (getsockname(srvr, (struct sockaddr *)sock_addr,
+                   &sock_addrlen) == -1)
+                       FAIL("getsockname");
+               print("sock", sock_addr, sock_addrlen);
 
-       len += OF;
-       if (sock_addrlen LX != len)
-               FAIL("sock_addr_len %zu != %zu", (size_t)sock_addrlen, len);
+               if (sock_addr->sun_family != AF_UNIX)
+                       FAIL("sock_addr->sun_family %d != AF_UNIX",
+                           sock_addr->sun_family);
+
+               len += OF;
+               if (sock_addrlen LX != len)
+                       FAIL("sock_addr_len %zu != %zu", (size_t)sock_addrlen,
+                           len);
 #ifdef BSD4_4
-       if (sock_addr->sun_len != sl)
-               FAIL("sock_addr.sun_len %d != %zu", sock_addr->sun_len,
-                   (size_t)sl);
+               if (sock_addr->sun_len != sl)
+                       FAIL("sock_addr.sun_len %d != %zu", sock_addr->sun_len,
+                           (size_t)sl);
 #endif
-       for (size_t i = 0; i < slen - OF; i++)
-               if (sock_addr->sun_path[i] != sun->sun_path[i])
-                       FAIL("sock_addr.sun_path[%zu] %d != "
-                           "sun->sun_path[%zu] %d\n", i, 
-                           sock_addr->sun_path[i], i, sun->sun_path[i]);
+               for (size_t i = 0; i < slen - OF; i++)
+                       if (sock_addr->sun_path[i] != sun->sun_path[i])
+                               FAIL("sock_addr.sun_path[%zu] %d != "
+                                   "sun->sun_path[%zu] %d\n", i, 
+                                   sock_addr->sun_path[i], i,
+                                   sun->sun_path[i]);
 
-       if (acpt != -1)
-               (void)close(acpt);
-       if (srvr != -1)
-               (void)close(srvr);
+               if (acpt != -1)
+                       (void)close(acpt);
+               if (srvr != -1)
+                       (void)close(srvr);
+               free(sock_addr);
+               sock_addr = NULL;
+               if (forkit && waitpid(clntpid, NULL, 0) == -1)
+                       FAIL("waitpid");
+       }
        if (clnt != -1 && !closeit)
                (void)close(clnt);
 
@@ -243,12 +274,16 @@
        free(sun);
        return 0;
 fail:
-       if (acpt != -1)
-               (void)close(acpt);
-       if (srvr != -1)
-               (void)close(srvr);
-       if (clnt != -1 && !closeit)
-               (void)close(clnt);
+       if (srvrpid == getpid()) {
+               if (acpt != -1)
+                       (void)close(acpt);
+               if (srvr != -1)
+                       (void)close(srvr);
+       }
+       if (clntpid == getpid()) {
+               if (clnt != -1 && !closeit)
+                       (void)close(clnt);
+       }
        free(sock_addr);
        free(sun);
        return -1;
@@ -267,8 +302,8 @@
 
 ATF_TC_BODY(sockaddr_un_len_exceed, tc)
 {
-       ATF_REQUIRE_MSG(test(false, 254) == -1, "test(false, 254): %s",
-           strerror(errno));
+       ATF_REQUIRE_MSG(test(false, false, 254) == -1,
+           "test(false, false, 254): %s", strerror(errno));
 }
 
 ATF_TC(sockaddr_un_len_max);
@@ -282,8 +317,8 @@
 
 ATF_TC_BODY(sockaddr_un_len_max, tc)
 {
-       ATF_REQUIRE_MSG(test(false, 253) == 0, "test(false, 253): %s",
-           strerror(errno));
+       ATF_REQUIRE_MSG(test(false, false, 253) == 0,
+           "test(false, false, 253): %s", strerror(errno));
 }
 
 ATF_TC(sockaddr_un_closed);
@@ -296,8 +331,22 @@
 
 ATF_TC_BODY(sockaddr_un_closed, tc)
 {
-       ATF_REQUIRE_MSG(test(true, 100) == 0, "test(true, 100): %s",
-           strerror(errno));
+       ATF_REQUIRE_MSG(test(false, true, 100) == 0,
+           "test(false, true, 100): %s", strerror(errno));
+}
+
+ATF_TC(sockaddr_un_local_peereid);
+ATF_TC_HEAD(sockaddr_un_local_peereid, tc)
+{
+
+       atf_tc_set_md_var(tc, "descr", "Check that we get the right information"
+           " from LOCAL_PEEREID");
+}
+
+ATF_TC_BODY(sockaddr_un_local_peereid, tc)
+{
+       ATF_REQUIRE_MSG(test(true, true, 100) == 0,
+           "test(true, true, 100): %s", strerror(errno));
 }
 
 ATF_TP_ADD_TCS(tp)
@@ -306,6 +355,7 @@
        ATF_TP_ADD_TC(tp, sockaddr_un_len_exceed);
        ATF_TP_ADD_TC(tp, sockaddr_un_len_max);
        ATF_TP_ADD_TC(tp, sockaddr_un_closed);
+       ATF_TP_ADD_TC(tp, sockaddr_un_local_peereid);
        return atf_no_error();
 }
 #else
@@ -318,7 +368,9 @@
                fprintf(stderr, "Usage: %s <len>\n", getprogname());
                return EXIT_FAILURE;
        }
-       test(false, atoi(argv[1]));
-       test(true, atoi(argv[1]));
+       test(false, false, atoi(argv[1]));
+       test(false, true, atoi(argv[1]));
+       test(true, false, atoi(argv[1]));
+       test(true, true, atoi(argv[1]));
 }
 #endif



Home | Main Index | Thread Index | Old Index