Linux Test Project
 help / color / mirror / Atom feed
From: Wei Gao via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v8] connect03: New test case for EPROTOTYPE and EACCES errors
Date: Mon,  6 Jul 2026 06:02:34 +0000	[thread overview]
Message-ID: <20260706060244.23332-1-wegao@suse.com> (raw)
In-Reply-To: <20260616093119.8654-3-wegao@suse.com>

Add a new connect03 test case targeting AF_UNIX-specific connect(2)
error paths, specifically EPROTOTYPE and EACCES.

- EPROTOTYPE is verified by attempting to connect a UNIX domain SOCK_DGRAM
  socket to a SOCK_STREAM listening server.
- EACCES is verified by modifying permissions on the socket file to 0700,
  spawning a child process, dropping privileges to the unprivileged "nobody"
  user, and attempting to connect.

Signed-off-by: Wei Gao <wegao@suse.com>
---
 runtest/syscalls                              |   1 +
 testcases/kernel/syscalls/connect/.gitignore  |   1 +
 testcases/kernel/syscalls/connect/connect03.c | 117 ++++++++++++++++++
 3 files changed, 119 insertions(+)
 create mode 100644 testcases/kernel/syscalls/connect/connect03.c

diff --git a/runtest/syscalls b/runtest/syscalls
index a021c79da..0fec2c6b5 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -142,6 +142,7 @@ confstr01 confstr01
 
 connect01 connect01
 connect02 connect02
+connect03 connect03
 
 creat01 creat01
 creat03 creat03
diff --git a/testcases/kernel/syscalls/connect/.gitignore b/testcases/kernel/syscalls/connect/.gitignore
index 0a3fc90bf..7ef5fef1a 100644
--- a/testcases/kernel/syscalls/connect/.gitignore
+++ b/testcases/kernel/syscalls/connect/.gitignore
@@ -1,2 +1,3 @@
 /connect01
 /connect02
+/connect03
diff --git a/testcases/kernel/syscalls/connect/connect03.c b/testcases/kernel/syscalls/connect/connect03.c
new file mode 100644
index 000000000..b576a10ce
--- /dev/null
+++ b/testcases/kernel/syscalls/connect/connect03.c
@@ -0,0 +1,117 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Linux Test Project
+ */
+
+/*\
+ * Verify that :manpage:`connect(2)` with AF_UNIX fails with -1 and sets proper errno:
+ *
+ * - EPROTOTYPE: The socket type does not support the protocol (e.g.,
+ *   connecting a UNIX domain datagram socket to a stream socket)
+ * - EACCES: Write permission is denied on the socket file
+ *
+ * Requires root to test EACCES by dropping privileges to an
+ * unprivileged user.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <pwd.h>
+#include <string.h>
+
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+
+#define SOCK_FILE "sock_file"
+
+static int fd_unix_server = -1;
+static int fd_unix_dgram = -1;
+static int fd_unix_stream = -1;
+
+static struct sockaddr_un sock_un;
+static struct passwd *pw;
+
+static struct test_case_t {
+	int *fd;
+	struct sockaddr_un *addr;
+	socklen_t salen;
+	int exp_errno;
+	const char *desc;
+} tcases[] = {
+	{
+		.fd = &fd_unix_dgram,
+		.addr = &sock_un,
+		.salen = sizeof(sock_un),
+		.exp_errno = EPROTOTYPE,
+		.desc = "socket type does not support the protocol"
+	},
+	{
+		.fd = &fd_unix_stream,
+		.addr = &sock_un,
+		.salen = sizeof(sock_un),
+		.exp_errno = EACCES,
+		.desc = "write permission is denied on the socket file"
+	},
+};
+
+static int sys_connect(int sockfd, const struct sockaddr *addr,
+		       socklen_t addrlen)
+{
+	return tst_syscall(__NR_connect, sockfd, addr, addrlen);
+}
+
+static void setup(void)
+{
+	sock_un.sun_family = AF_UNIX;
+	strncpy(sock_un.sun_path, SOCK_FILE, sizeof(sock_un.sun_path));
+
+	fd_unix_server = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0);
+	SAFE_BIND(fd_unix_server, (struct sockaddr *)&sock_un, sizeof(sock_un));
+	SAFE_CHMOD(SOCK_FILE, 0700);
+	SAFE_LISTEN(fd_unix_server, 5);
+
+	fd_unix_dgram = SAFE_SOCKET(AF_UNIX, SOCK_DGRAM, 0);
+	fd_unix_stream = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0);
+
+	pw = SAFE_GETPWNAM("nobody");
+}
+
+static void cleanup(void)
+{
+	if (fd_unix_dgram != -1)
+		SAFE_CLOSE(fd_unix_dgram);
+	if (fd_unix_stream != -1)
+		SAFE_CLOSE(fd_unix_stream);
+	if (fd_unix_server != -1)
+		SAFE_CLOSE(fd_unix_server);
+}
+
+static void verify_connect(unsigned int i)
+{
+	struct test_case_t *tc = &tcases[i];
+
+	if (tc->exp_errno == EACCES) {
+		if (!SAFE_FORK()) {
+			SAFE_SETUID(pw->pw_uid);
+			TST_EXP_FAIL(sys_connect(*tc->fd, (const struct sockaddr *)tc->addr, tc->salen),
+				     tc->exp_errno, "%s", tc->desc);
+			exit(0);
+		}
+		tst_reap_children();
+	} else {
+		TST_EXP_FAIL(sys_connect(*tc->fd, (const struct sockaddr *)tc->addr, tc->salen),
+			     tc->exp_errno, "%s", tc->desc);
+	}
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_connect,
+	.forks_child = 1,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+};
-- 
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  parent reply	other threads:[~2026-07-06  6:03 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-14  4:51 [LTP] [PATCH v2 1/2] connect01: Convert to new API Yang Xu via ltp
2024-05-14  4:51 ` [LTP] [PATCH v2 2/2] connect01: Add negative tests Yang Xu via ltp
2024-07-17 12:30 ` [LTP] [PATCH v2 1/2] connect01: Convert to new API Cyril Hrubis
2026-05-25  9:02 ` [LTP] [PATCH v3 0/2] " Wei Gao via ltp
2026-05-25  9:02   ` [LTP] [PATCH v3 1/2] " Wei Gao via ltp
2026-05-25 10:12     ` [LTP] " linuxtestproject.agent
2026-05-28  4:43       ` Wei Gao via ltp
2026-06-04  4:49     ` [LTP] [PATCH v4 0/2] " Wei Gao via ltp
2026-06-04  4:49       ` [LTP] [PATCH v4 1/2] " Wei Gao via ltp
2026-06-04  7:15         ` [LTP] " linuxtestproject.agent
2026-06-16  1:47         ` [LTP] [PATCH v5 0/2] " Wei Gao via ltp
2026-06-16  1:47           ` [LTP] [PATCH v5 1/2] " Wei Gao via ltp
2026-06-16  4:07             ` [LTP] " linuxtestproject.agent
2026-06-16  5:24             ` [LTP] [PATCH v6 0/2] " Wei Gao via ltp
2026-06-16  5:24               ` [LTP] [PATCH v6 1/2] " Wei Gao via ltp
2026-06-16  8:35                 ` [LTP] " linuxtestproject.agent
2026-06-16  9:31                 ` [LTP] [PATCH v7 0/2] " Wei Gao via ltp
2026-06-16  9:31                   ` [LTP] [PATCH v7 1/2] " Wei Gao via ltp
2026-06-16 10:12                     ` [LTP] " linuxtestproject.agent
2026-07-01  8:34                     ` [LTP] [PATCH v7 1/2] " Petr Vorel
2026-06-16  9:31                   ` [LTP] [PATCH v7 2/2] connect01: Add negative tests Wei Gao via ltp
2026-07-01  8:38                     ` Petr Vorel
2026-07-06  6:02                     ` Wei Gao via ltp [this message]
2026-07-06  8:22                       ` [LTP] connect03: New test case for EPROTOTYPE and EACCES errors linuxtestproject.agent
2026-07-06 23:39                       ` [LTP] [PATCH v9] " Wei Gao via ltp
2026-07-07  3:25                         ` [LTP] " linuxtestproject.agent
2026-06-16  5:24               ` [LTP] [PATCH v6 2/2] connect01: Add negative tests Wei Gao via ltp
2026-06-16  1:47           ` [LTP] [PATCH v5 " Wei Gao via ltp
2026-06-04  4:49       ` [LTP] [PATCH v4 " Wei Gao via ltp
2026-05-25  9:02   ` [LTP] [PATCH v3 " Wei Gao via ltp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260706060244.23332-1-wegao@suse.com \
    --to=ltp@lists.linux.it \
    --cc=wegao@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox