Skip to contentSkip to conversation

getsockopt: Add test for SO_PEERCRED

PATCH Superseded [email protected] 2 participants 2 messages Jul 24, 2017 – Jul 25, 2017

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 getsockopt: Add test for SO_PEERCRED

From: Veronika Kabatova <[email protected]>

Connect to a socket from a fork and check SO_PEERCRED returns correct PID.

---
 runtest/syscalls                                   |   1 +
 testcases/kernel/syscalls/.gitignore               |   1 +
 testcases/kernel/syscalls/getsockopt/Makefile      |   2 +
 .../kernel/syscalls/getsockopt/getsockopt02.c      | 106 +++++++++++++++++++++
 4 files changed, 110 insertions(+)
 create mode 100644 testcases/kernel/syscalls/getsockopt/getsockopt02.c
runtest/syscalls +1 -0
index 2e79ab9..c85c713 100644
@@ -434,6 +434,7 @@ getsid02 getsid02
 getsockname01 getsockname01
 
 getsockopt01 getsockopt01
+getsockopt02 getsockopt02
 
 gettid01 gettid01
 
testcases/kernel/syscalls/.gitignore +1 -0
index fdb01f9..958bd7a 100644
@@ -362,6 +362,7 @@
 /getsid/getsid02
 /getsockname/getsockname01
 /getsockopt/getsockopt01
+/getsockopt/getsockopt02
 /gettid/gettid01
 /gettimeofday/gettimeofday01
 /gettimeofday/gettimeofday02
testcases/kernel/syscalls/getsockopt/Makefile +2 -0
index bd617d8..033d73f 100644
@@ -18,6 +18,8 @@
 
 top_srcdir		?= ../../../..
 
+getsockopt02: CFLAGS+=-pthread
+
 include $(top_srcdir)/include/mk/testcases.mk
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/getsockopt/getsockopt02.c new file +106 -0

106 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/1 getsockopt: Add test for SO_PEERCRED

Hi Veronika,

testcases/kernel/syscalls/getsockopt/Makefile line 22

Quoted 10 lines · [email protected] writes:
testcases/kernel/syscalls/getsockopt/Makefile +2 -0
index bd617d8..033d73f 100644
@@ -18,6 +18,8 @@
 
 top_srcdir		?= ../../../..
 
+getsockopt02: CFLAGS+=-pthread
+

You don't need this anymore.

Quoted 46 lines
testcases/kernel/syscalls/getsockopt/Makefile +0 -0
 include $(top_srcdir)/include/mk/testcases.mk
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/getsockopt/getsockopt02.c new file +37 -0
new file mode 100644
index 0000000..50e9034
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2017 Red Hat, Inc.
+ *
+ * This program is free software;  you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * Test description: Test retrieving of peer credentials (SO_PEERCRED)
+ *
+ */
+
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <stdlib.h>
+#include "tst_test.h"
+
+static int socket_fd, fork_socket_fd, accepted;
+static struct sockaddr_un sun;
+
+#define SOCKNAME	"testsocket"
+
+static void setup(void)
+{
+	sun.sun_family = AF_UNIX;
+	(void)strcpy(sun.sun_path, SOCKNAME);

testcases/kernel/syscalls/getsockopt/getsockopt02.c line 47

Quoted 10 lines
testcases/kernel/syscalls/getsockopt/getsockopt02.c new file +10 -0
+	socket_fd = SAFE_SOCKET(sun.sun_family, SOCK_STREAM, 0);
+	SAFE_BIND(socket_fd, (struct sockaddr *)&sun, sizeof(sun));
+	SAFE_LISTEN(socket_fd, SOMAXCONN);
+}
+
+static void fork_func(void)
+{
+	fork_socket_fd = SAFE_SOCKET(sun.sun_family, SOCK_STREAM, 0);
+	SAFE_CONNECT(fork_socket_fd, (struct sockaddr *)&sun, sizeof(sun));
+}

To avoid race conditions you probably want to keep the child process alive until the parent has read the peer creds.

https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines#229-fork-and-parent-child-synchronization

Quoted 5 lines
testcases/kernel/syscalls/getsockopt/getsockopt02.c new file +5 -0
+
+static void test_function(void)
+{
+	pid_t fork_id;
+	struct ucred cred;

testcases/kernel/syscalls/getsockopt/getsockopt02.c line 62

Quoted 10 lines
testcases/kernel/syscalls/getsockopt/getsockopt02.c new file +10 -0
+	int status;
+	socklen_t cred_len = sizeof(cred);
+
+	fork_id = SAFE_FORK();
+	if (!fork_id) {
+		fork_func();
+		exit(0);
+	}
+
+	SAFE_WAITPID(fork_id, &status, 0);

I'm surprised this works. It is waiting for the child to exit (or receive some other signal, but I can't see what) then accepts the connection from the child, but after the child is dead. If it works then I would guess that some race is happening within the kernel between socket and process cleanup.

Quoted 7 lines
testcases/kernel/syscalls/getsockopt/getsockopt02.c new file +7 -0
+	accepted = accept(socket_fd, NULL, NULL);
+	if (accepted < 0) {
+		tst_res(TFAIL | TERRNO, "Error with accepting connection");
+		return;
+	}
+	if (getsockopt(accepted, SOL_SOCKET,
+				SO_PEERCRED, &cred, &cred_len) < 0) {

testcases/kernel/syscalls/getsockopt/getsockopt02.c line 79

Quoted 10 lines
testcases/kernel/syscalls/getsockopt/getsockopt02.c new file +10 -0
+		tst_res(TFAIL | TERRNO, "Error while getting socket options");
+		return;
+	}
+
+	if (accepted >= 0) {
+		(void)shutdown(accepted, SHUT_RDWR);
+		SAFE_CLOSE(accepted);
+	}
+	if (fork_socket_fd >= 0)
+		SAFE_CLOSE(fork_socket_fd);

Unlike with threads, fork does not share memory with the child process. So fork_socket_fd will always be zero in the parent. You could just call SAFE_CLOSE at the end of fork_func().

testcases/kernel/syscalls/getsockopt/getsockopt02.c line 91

Quoted 12 lines
testcases/kernel/syscalls/getsockopt/getsockopt02.c new file +12 -0
+
+	if (fork_id != cred.pid) {
+		tst_res(TFAIL, "Received wrong PID %d, expected %d",
+				cred.pid, getpid());
+	} else
+		tst_res(TPASS, "Test passed");
+}
+
+static void cleanup(void)
+{
+	if (accepted >= 0) {
+		(void)shutdown(accepted, SHUT_RDWR);

This is probably overkill.

testcases/kernel/syscalls/getsockopt/getsockopt02.c line 79

Quoted 4 lines
testcases/kernel/syscalls/getsockopt/getsockopt02.c new file +4 -0
+		SAFE_CLOSE(accepted);
+	}
+	if (fork_socket_fd >= 0)
+		SAFE_CLOSE(fork_socket_fd);

Again this will always be zero as cleanup() is not called by the child.

Quoted 12 lines
testcases/kernel/syscalls/getsockopt/getsockopt02.c new file +11 -1
+	if (socket_fd >= 0)
+		SAFE_CLOSE(socket_fd);
+}
+
+static struct tst_test test = {
+	.test_all = test_function,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+	.forks_child = 1,
+};
--
Quoted 1 line

2.7.4

--
Thank you,
Richard.