[LTP] [PATCH v10] connect03: New test case for EPROTOTYPE and EACCES errors
Wei Gao
wegao@suse.com
Fri Jul 10 07:13:11 CEST 2026
connect01/02 only cover generic socket error paths and do not test
AF_UNIX-specific errors.
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 setting socket file permissions to 0500 and
attempting to connect. If run as root, effective privileges are
dynamically dropped to 'nobody' in setup() to ensure standard DAC
checks apply and prevent CAP_DAC_OVERRIDE from bypassing permissions.
Signed-off-by: Wei Gao <wegao@suse.com>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/connect/.gitignore | 1 +
testcases/kernel/syscalls/connect/connect03.c | 99 +++++++++++++++++++
3 files changed, 101 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..47f0347d3
--- /dev/null
+++ b/testcases/kernel/syscalls/connect/connect03.c
@@ -0,0 +1,99 @@
+// 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
+ */
+
+#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"
+
+#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 test_case_t {
+ int *fd;
+ int exp_errno;
+ mode_t mode;
+ const char *desc;
+} tcases[] = {
+ {
+ .fd = &fd_unix_dgram,
+ .exp_errno = EPROTOTYPE,
+ .mode = 0700,
+ .desc = "socket type does not support the protocol"
+ },
+ {
+ .fd = &fd_unix_stream,
+ .exp_errno = EACCES,
+ .mode = 0500,
+ .desc = "write permission is denied on the socket file"
+ },
+};
+
+static void setup(void)
+{
+ struct passwd *pw;
+
+ /* Drop privileges to 'nobody' if we are running as root */
+ if (geteuid() == 0) {
+ pw = SAFE_GETPWNAM("nobody");
+ SAFE_SETEUID(pw->pw_uid);
+ }
+
+ 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);
+}
+
+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];
+
+ SAFE_CHMOD(SOCK_FILE, tc->mode);
+
+ TST_EXP_FAIL(connect(*tc->fd, (const struct sockaddr *)&sock_un, sizeof(sock_un)),
+ tc->exp_errno, "%s", tc->desc);
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .cleanup = cleanup,
+ .tcnt = ARRAY_SIZE(tcases),
+ .test = verify_connect,
+ .needs_tmpdir = 1,
+};
--
2.54.0
More information about the ltp
mailing list