Blame SOURCES/0179-secontext-print-context-of-Unix-socket-s-sun_path-fi.patch

024872
From 960e78f208b4f6d48962bbc9cad45588cc8c90ad Mon Sep 17 00:00:00 2001
024872
From: =?UTF-8?q?Renaud=20M=C3=A9trich?= <rmetrich@redhat.com>
024872
Date: Tue, 21 Jun 2022 08:43:00 +0200
024872
Subject: [PATCH] secontext: print context of Unix socket's sun_path field
024872
MIME-Version: 1.0
024872
Content-Type: text/plain; charset=UTF-8
024872
Content-Transfer-Encoding: 8bit
024872
024872
Signed-off-by: Renaud Métrich <rmetrich@redhat.com>
024872
024872
* src/sockaddr.c: Include "secontext.h".
024872
(print_sockaddr_data_un): Print the SELinux context of sun_path field
024872
using selinux_printfilecon.
024872
* NEWS: Mention this change.
024872
* tests/secontext.c (raw_secontext_full_fd, get_secontext_field_fd,
024872
raw_secontext_short_fd, secontext_full_fd, secontext_short_fd): New
024872
functions.
024872
* tests/secontext.h (secontext_full_fd, secontext_short_fd,
024872
get_secontext_field_fd): New prototypes.
024872
(SECONTEXT_FD): New macro.
024872
* tests/sockname.c: Include "secontext.h".
024872
(test_sockname_syscall): Update expected output.
024872
* tests/gen_tests.in (getsockname--secontext,
024872
getsockname--secontext_full, getsockname--secontext_full_mismatch,
024872
getsockname--secontext_mismatch): New tests.
024872
024872
Resolves: https://github.com/strace/strace/pull/214
024872
---
024872
 NEWS               |  1 +
024872
 src/sockaddr.c     |  3 +++
024872
 tests/gen_tests.in |  4 ++++
024872
 tests/secontext.c  | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
024872
 tests/secontext.h  | 12 ++++++++++++
024872
 tests/sockname.c   | 54 +++++++++++++++++++++++++++++++++++-------------------
024872
 6 files changed, 104 insertions(+), 19 deletions(-)
024872
024872
Index: strace-5.18/NEWS
024872
===================================================================
024872
--- strace-5.18.orig/NEWS	2022-07-12 18:20:18.495470531 +0200
024872
+++ strace-5.18/NEWS	2022-07-12 18:20:44.531163262 +0200
024872
@@ -5,6 +5,7 @@
024872
   * Added an interface of raising des Strausses awareness.
024872
   * Added --tips option to print strace tips, tricks, and tweaks
024872
     at the end of the tracing session.
024872
+  * Implemented printing of Unix socket sun_path field's SELinux context.
024872
   * Enhanced decoding of bpf and io_uring_register syscalls.
024872
   * Implemented decoding of COUNTER_*, RTC_PARAM_GET, and RTC_PARAM_SET ioctl
024872
     commands.
024872
Index: strace-5.18/src/sockaddr.c
024872
===================================================================
024872
--- strace-5.18.orig/src/sockaddr.c	2022-07-12 18:17:36.745379483 +0200
024872
+++ strace-5.18/src/sockaddr.c	2022-07-12 18:20:18.495470531 +0200
024872
@@ -63,6 +63,8 @@
024872
 #include "xlat/mctp_addrs.h"
024872
 #include "xlat/mctp_nets.h"
024872
 
024872
+#include "secontext.h"
024872
+
024872
 #define SIZEOF_SA_FAMILY sizeof_field(struct sockaddr, sa_family)
024872
 
024872
 struct sockaddr_rxrpc {
024872
@@ -115,6 +117,7 @@
024872
 	if (sa_un->sun_path[0]) {
024872
 		print_quoted_string(sa_un->sun_path, path_len + 1,
024872
 				    QUOTE_0_TERMINATED);
024872
+		selinux_printfilecon(tcp, sa_un->sun_path);
024872
 	} else {
024872
 		tprints("@");
024872
 		print_quoted_string(sa_un->sun_path + 1, path_len - 1, 0);
024872
Index: strace-5.18/tests/gen_tests.in
024872
===================================================================
024872
--- strace-5.18.orig/tests/gen_tests.in	2022-07-12 18:17:36.746379471 +0200
024872
+++ strace-5.18/tests/gen_tests.in	2022-07-12 18:20:18.496470519 +0200
024872
@@ -225,6 +225,10 @@
024872
 getsid	-a10
024872
 getsid--pidns-translation	test_pidns -e trace=getsid -a10
024872
 getsockname	-a27
024872
+getsockname--secontext	-a27 --secontext -e trace=getsockname
024872
+getsockname--secontext_full	-a27 --secontext=full -e trace=getsockname
024872
+getsockname--secontext_full_mismatch	-a27 --secontext=full,mismatch -e trace=getsockname
024872
+getsockname--secontext_mismatch	-a27 --secontext=mismatch -e trace=getsockname
024872
 gettid	-a9
024872
 getuid-creds	+getuid.test
024872
 getuid32	+getuid.test
024872
Index: strace-5.18/tests/secontext.c
024872
===================================================================
024872
--- strace-5.18.orig/tests/secontext.c	2022-07-12 18:17:36.747379459 +0200
024872
+++ strace-5.18/tests/secontext.c	2022-07-12 18:20:18.496470519 +0200
024872
@@ -141,6 +141,21 @@
024872
 	return full_secontext;
024872
 }
024872
 
024872
+static char *
024872
+raw_secontext_full_fd(int fd)
024872
+{
024872
+	int saved_errno = errno;
024872
+	char *full_secontext = NULL;
024872
+	char *secontext;
024872
+
024872
+	if (fgetfilecon(fd, &secontext) >= 0) {
024872
+		full_secontext = strip_trailing_newlines(xstrdup(secontext));
024872
+		freecon(secontext);
024872
+	}
024872
+	errno = saved_errno;
024872
+	return full_secontext;
024872
+}
024872
+
024872
 char *
024872
 get_secontext_field_file(const char *file, enum secontext_field field)
024872
 {
024872
@@ -151,6 +166,16 @@
024872
 	return type;
024872
 }
024872
 
024872
+char *
024872
+get_secontext_field_fd(int fd, enum secontext_field field)
024872
+{
024872
+	char *ctx = raw_secontext_full_fd(fd);
024872
+	char *type =  get_secontext_field(ctx, field);
024872
+	free(ctx);
024872
+
024872
+	return type;
024872
+}
024872
+
024872
 static char *
024872
 raw_secontext_short_file(const char *filename)
024872
 {
024872
@@ -158,6 +183,12 @@
024872
 }
024872
 
024872
 static char *
024872
+raw_secontext_short_fd(int fd)
024872
+{
024872
+	return get_secontext_field_fd(fd, SECONTEXT_TYPE);
024872
+}
024872
+
024872
+static char *
024872
 raw_secontext_full_pid(pid_t pid)
024872
 {
024872
 	int saved_errno = errno;
024872
@@ -205,6 +236,15 @@
024872
 }
024872
 
024872
 char *
024872
+secontext_full_fd(int fd)
024872
+{
024872
+	int saved_errno = errno;
024872
+	char *context = raw_secontext_full_fd(fd);
024872
+	errno = saved_errno;
024872
+	return FORMAT_SPACE_BEFORE(context);
024872
+}
024872
+
024872
+char *
024872
 secontext_full_pid(pid_t pid)
024872
 {
024872
 	return FORMAT_SPACE_AFTER(raw_secontext_full_pid(pid));
024872
@@ -228,6 +268,15 @@
024872
 	errno = saved_errno;
024872
 	return FORMAT_SPACE_BEFORE(context);
024872
 }
024872
+
024872
+char *
024872
+secontext_short_fd(int fd)
024872
+{
024872
+	int saved_errno = errno;
024872
+	char *context = raw_secontext_short_fd(fd);
024872
+	errno = saved_errno;
024872
+	return FORMAT_SPACE_BEFORE(context);
024872
+}
024872
 
024872
 char *
024872
 secontext_short_pid(pid_t pid)
024872
Index: strace-5.18/tests/secontext.h
024872
===================================================================
024872
--- strace-5.18.orig/tests/secontext.h	2022-07-12 18:17:36.747379459 +0200
024872
+++ strace-5.18/tests/secontext.h	2022-07-12 18:20:18.496470519 +0200
024872
@@ -9,9 +9,11 @@
024872
 #include "xmalloc.h"
024872
 #include <unistd.h>
024872
 
024872
+char *secontext_full_fd(int) ATTRIBUTE_MALLOC;
024872
 char *secontext_full_file(const char *, bool) ATTRIBUTE_MALLOC;
024872
 char *secontext_full_pid(pid_t) ATTRIBUTE_MALLOC;
024872
 
024872
+char *secontext_short_fd(int) ATTRIBUTE_MALLOC;
024872
 char *secontext_short_file(const char *, bool) ATTRIBUTE_MALLOC;
024872
 char *secontext_short_pid(pid_t) ATTRIBUTE_MALLOC;
024872
 
024872
@@ -30,6 +32,7 @@
024872
  */
024872
 char *get_secontext_field(const char *full_context, enum secontext_field field);
024872
 
024872
+char *get_secontext_field_fd(int fd, enum secontext_field field);
024872
 char *get_secontext_field_file(const char *file, enum secontext_field field);
024872
 
024872
 void reset_secontext_file(const char *file);
024872
@@ -44,6 +47,7 @@
024872
 #  else
024872
 #   define SECONTEXT_FILE(filename)	secontext_full_file(filename, false)
024872
 #  endif
024872
+#  define SECONTEXT_FD(fd)		secontext_full_fd(fd)
024872
 #  define SECONTEXT_PID(pid)		secontext_full_pid(pid)
024872
 
024872
 # else
024872
@@ -53,6 +57,7 @@
024872
 #  else
024872
 #   define SECONTEXT_FILE(filename)	secontext_short_file(filename, false)
024872
 #  endif
024872
+#  define SECONTEXT_FD(fd)		secontext_short_fd(fd)
024872
 #  define SECONTEXT_PID(pid)		secontext_short_pid(pid)
024872
 
024872
 # endif
024872
@@ -65,6 +70,12 @@
024872
 	return NULL;
024872
 }
024872
 static inline char *
024872
+get_secontext_field_fd(int fd, enum secontext_field field)
024872
+{
024872
+	return NULL;
024872
+}
024872
+
024872
+static inline char *
024872
 get_secontext_field_file(const char *file, enum secontext_field field)
024872
 {
024872
 	return NULL;
024872
@@ -81,6 +92,7 @@
024872
 {
024872
 }
024872
 
024872
+# define SECONTEXT_FD(fd)			xstrdup("")
024872
 # define SECONTEXT_FILE(filename)		xstrdup("")
024872
 # define SECONTEXT_PID(pid)			xstrdup("")
024872
 
024872
Index: strace-5.18/tests/sockname.c
024872
===================================================================
024872
--- strace-5.18.orig/tests/sockname.c	2022-07-12 18:17:36.748379448 +0200
024872
+++ strace-5.18/tests/sockname.c	2022-07-12 18:20:18.496470519 +0200
024872
@@ -18,6 +18,8 @@
024872
 #include <sys/socket.h>
024872
 #include <sys/un.h>
024872
 
024872
+#include "secontext.h"
024872
+
024872
 #ifndef TEST_SYSCALL_NAME
024872
 # error TEST_SYSCALL_NAME must be defined
024872
 #endif
024872
@@ -59,14 +61,19 @@
024872
 	*plen = sizeof(struct sockaddr_un);
024872
 	struct sockaddr_un *addr = tail_alloc(*plen);
024872
 
024872
+	char *my_secontext = SECONTEXT_PID_MY();
024872
+	char *fd_secontext = SECONTEXT_FD(fd);
024872
+
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	int rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr,
024872
 				   plen SUFFIX_ARGS);
024872
 	if (rc < 0)
024872
 		perror_msg_and_skip(TEST_SYSCALL_STR);
024872
-	printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%s\"}"
024872
+	printf("%s%s(%d%s%s, {sa_family=AF_UNIX, sun_path=\"%s\"%s}"
024872
 	       ", [%d => %d]%s) = %d\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_S_STR, addr->sun_path,
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
024872
+	       addr->sun_path, SECONTEXT_FILE(addr->sun_path),
024872
 	       (int) sizeof(struct sockaddr_un), (int) *plen, SUFFIX_STR, rc);
024872
 
024872
 	memset(addr, 0, sizeof(*addr));
024872
@@ -75,28 +82,34 @@
024872
 			       plen SUFFIX_ARGS);
024872
 	if (rc < 0)
024872
 		perror_msg_and_skip(TEST_SYSCALL_STR);
024872
-	printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%s\"}"
024872
+	printf("%s%s(%d%s%s, {sa_family=AF_UNIX, sun_path=\"%s\"%s}"
024872
 	       ", [%d]%s) = %d\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_S_STR, addr->sun_path,
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
024872
+	       addr->sun_path, SECONTEXT_FILE(addr->sun_path),
024872
 	       (int) *plen, SUFFIX_STR, rc);
024872
 
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr, 0 SUFFIX_ARGS);
024872
-	printf("%s(%d%s, %p, NULL%s) = %s\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr, SUFFIX_STR,
024872
-	       sprintrc(rc));
024872
+	printf("%s%s(%d%s%s, %p, NULL%s) = %s\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_F_STR,
024872
+	       addr, SUFFIX_STR, sprintrc(rc));
024872
 
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, 0, 0 SUFFIX_ARGS);
024872
-	printf("%s(%d%s, NULL, NULL%s) = %s\n",
024872
-	       TEST_SYSCALL_STR, fd, rc == -1 ? PREFIX_F_STR : PREFIX_S_STR,
024872
+	printf("%s%s(%d%s%s, NULL, NULL%s) = %s\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext,
024872
+	       rc == -1 ? PREFIX_F_STR : PREFIX_S_STR,
024872
 	       SUFFIX_STR, sprintrc(rc));
024872
 
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr,
024872
 			       plen + 1 SUFFIX_ARGS);
024872
-	printf("%s(%d%s, %p, %p%s) = %s\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr,
024872
+	printf("%s%s(%d%s%s, %p, %p%s) = %s\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_F_STR, addr,
024872
 	       plen + 1, SUFFIX_STR, sprintrc(rc));
024872
 
024872
 	const size_t offsetof_sun_path = offsetof(struct sockaddr_un, sun_path);
024872
@@ -108,8 +121,9 @@
024872
 			       plen SUFFIX_ARGS);
024872
 	if (rc < 0)
024872
 		perror_msg_and_skip(TEST_SYSCALL_STR);
024872
-	printf("%s(%d%s, {sa_family=AF_UNIX}, [%d => %d]%s) = %d\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_S_STR,
024872
+	printf("%s%s(%d%s%s, {sa_family=AF_UNIX}, [%d => %d]%s) = %d\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
024872
 	       (int) offsetof_sun_path, (int) *plen, SUFFIX_STR, rc);
024872
 
024872
 	++addr;
024872
@@ -121,17 +135,19 @@
024872
 			       plen SUFFIX_ARGS);
024872
 	if (rc < 0)
024872
 		perror_msg_and_skip(TEST_SYSCALL_STR);
024872
-	printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%.*s\"}"
024872
+	printf("%s%s(%d%s%s, {sa_family=AF_UNIX, sun_path=\"%.*s\"%s}"
024872
 	       ", [%d => %d]%s) = %d\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_S_STR,
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
024872
 	       (int) (sizeof(struct sockaddr) - offsetof_sun_path),
024872
-	       addr->sun_path, (int) sizeof(struct sockaddr),
024872
-	       (int) *plen, SUFFIX_STR, rc);
024872
+	       addr->sun_path, SECONTEXT_FILE(addr->sun_path),
024872
+	       (int) sizeof(struct sockaddr), (int) *plen, SUFFIX_STR, rc);
024872
 
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr,
024872
 			       plen SUFFIX_ARGS);
024872
-	printf("%s(%d%s, %p, [%d]%s) = %s\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr,
024872
+	printf("%s%s(%d%s%s, %p, [%d]%s) = %s\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_F_STR, addr,
024872
 	       *plen, SUFFIX_STR, sprintrc(rc));
024872
 }
024872
Index: strace-5.18/tests-m32/secontext.c
024872
===================================================================
024872
--- strace-5.18.orig/tests-m32/secontext.c	2022-07-12 18:17:36.747379459 +0200
024872
+++ strace-5.18/tests-m32/secontext.c	2022-07-12 18:20:18.496470519 +0200
024872
@@ -141,6 +141,21 @@
024872
 	return full_secontext;
024872
 }
024872
 
024872
+static char *
024872
+raw_secontext_full_fd(int fd)
024872
+{
024872
+	int saved_errno = errno;
024872
+	char *full_secontext = NULL;
024872
+	char *secontext;
024872
+
024872
+	if (fgetfilecon(fd, &secontext) >= 0) {
024872
+		full_secontext = strip_trailing_newlines(xstrdup(secontext));
024872
+		freecon(secontext);
024872
+	}
024872
+	errno = saved_errno;
024872
+	return full_secontext;
024872
+}
024872
+
024872
 char *
024872
 get_secontext_field_file(const char *file, enum secontext_field field)
024872
 {
024872
@@ -151,6 +166,16 @@
024872
 	return type;
024872
 }
024872
 
024872
+char *
024872
+get_secontext_field_fd(int fd, enum secontext_field field)
024872
+{
024872
+	char *ctx = raw_secontext_full_fd(fd);
024872
+	char *type =  get_secontext_field(ctx, field);
024872
+	free(ctx);
024872
+
024872
+	return type;
024872
+}
024872
+
024872
 static char *
024872
 raw_secontext_short_file(const char *filename)
024872
 {
024872
@@ -158,6 +183,12 @@
024872
 }
024872
 
024872
 static char *
024872
+raw_secontext_short_fd(int fd)
024872
+{
024872
+	return get_secontext_field_fd(fd, SECONTEXT_TYPE);
024872
+}
024872
+
024872
+static char *
024872
 raw_secontext_full_pid(pid_t pid)
024872
 {
024872
 	int saved_errno = errno;
024872
@@ -205,6 +236,15 @@
024872
 }
024872
 
024872
 char *
024872
+secontext_full_fd(int fd)
024872
+{
024872
+	int saved_errno = errno;
024872
+	char *context = raw_secontext_full_fd(fd);
024872
+	errno = saved_errno;
024872
+	return FORMAT_SPACE_BEFORE(context);
024872
+}
024872
+
024872
+char *
024872
 secontext_full_pid(pid_t pid)
024872
 {
024872
 	return FORMAT_SPACE_AFTER(raw_secontext_full_pid(pid));
024872
@@ -228,6 +268,15 @@
024872
 	errno = saved_errno;
024872
 	return FORMAT_SPACE_BEFORE(context);
024872
 }
024872
+
024872
+char *
024872
+secontext_short_fd(int fd)
024872
+{
024872
+	int saved_errno = errno;
024872
+	char *context = raw_secontext_short_fd(fd);
024872
+	errno = saved_errno;
024872
+	return FORMAT_SPACE_BEFORE(context);
024872
+}
024872
 
024872
 char *
024872
 secontext_short_pid(pid_t pid)
024872
Index: strace-5.18/tests-m32/secontext.h
024872
===================================================================
024872
--- strace-5.18.orig/tests-m32/secontext.h	2022-07-12 18:17:36.747379459 +0200
024872
+++ strace-5.18/tests-m32/secontext.h	2022-07-12 18:20:18.496470519 +0200
024872
@@ -9,9 +9,11 @@
024872
 #include "xmalloc.h"
024872
 #include <unistd.h>
024872
 
024872
+char *secontext_full_fd(int) ATTRIBUTE_MALLOC;
024872
 char *secontext_full_file(const char *, bool) ATTRIBUTE_MALLOC;
024872
 char *secontext_full_pid(pid_t) ATTRIBUTE_MALLOC;
024872
 
024872
+char *secontext_short_fd(int) ATTRIBUTE_MALLOC;
024872
 char *secontext_short_file(const char *, bool) ATTRIBUTE_MALLOC;
024872
 char *secontext_short_pid(pid_t) ATTRIBUTE_MALLOC;
024872
 
024872
@@ -30,6 +32,7 @@
024872
  */
024872
 char *get_secontext_field(const char *full_context, enum secontext_field field);
024872
 
024872
+char *get_secontext_field_fd(int fd, enum secontext_field field);
024872
 char *get_secontext_field_file(const char *file, enum secontext_field field);
024872
 
024872
 void reset_secontext_file(const char *file);
024872
@@ -44,6 +47,7 @@
024872
 #  else
024872
 #   define SECONTEXT_FILE(filename)	secontext_full_file(filename, false)
024872
 #  endif
024872
+#  define SECONTEXT_FD(fd)		secontext_full_fd(fd)
024872
 #  define SECONTEXT_PID(pid)		secontext_full_pid(pid)
024872
 
024872
 # else
024872
@@ -53,6 +57,7 @@
024872
 #  else
024872
 #   define SECONTEXT_FILE(filename)	secontext_short_file(filename, false)
024872
 #  endif
024872
+#  define SECONTEXT_FD(fd)		secontext_short_fd(fd)
024872
 #  define SECONTEXT_PID(pid)		secontext_short_pid(pid)
024872
 
024872
 # endif
024872
@@ -65,6 +70,12 @@
024872
 	return NULL;
024872
 }
024872
 static inline char *
024872
+get_secontext_field_fd(int fd, enum secontext_field field)
024872
+{
024872
+	return NULL;
024872
+}
024872
+
024872
+static inline char *
024872
 get_secontext_field_file(const char *file, enum secontext_field field)
024872
 {
024872
 	return NULL;
024872
@@ -81,6 +92,7 @@
024872
 {
024872
 }
024872
 
024872
+# define SECONTEXT_FD(fd)			xstrdup("")
024872
 # define SECONTEXT_FILE(filename)		xstrdup("")
024872
 # define SECONTEXT_PID(pid)			xstrdup("")
024872
 
024872
Index: strace-5.18/tests-m32/sockname.c
024872
===================================================================
024872
--- strace-5.18.orig/tests-m32/sockname.c	2022-07-12 18:17:36.748379448 +0200
024872
+++ strace-5.18/tests-m32/sockname.c	2022-07-12 18:20:18.496470519 +0200
024872
@@ -18,6 +18,8 @@
024872
 #include <sys/socket.h>
024872
 #include <sys/un.h>
024872
 
024872
+#include "secontext.h"
024872
+
024872
 #ifndef TEST_SYSCALL_NAME
024872
 # error TEST_SYSCALL_NAME must be defined
024872
 #endif
024872
@@ -59,14 +61,19 @@
024872
 	*plen = sizeof(struct sockaddr_un);
024872
 	struct sockaddr_un *addr = tail_alloc(*plen);
024872
 
024872
+	char *my_secontext = SECONTEXT_PID_MY();
024872
+	char *fd_secontext = SECONTEXT_FD(fd);
024872
+
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	int rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr,
024872
 				   plen SUFFIX_ARGS);
024872
 	if (rc < 0)
024872
 		perror_msg_and_skip(TEST_SYSCALL_STR);
024872
-	printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%s\"}"
024872
+	printf("%s%s(%d%s%s, {sa_family=AF_UNIX, sun_path=\"%s\"%s}"
024872
 	       ", [%d => %d]%s) = %d\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_S_STR, addr->sun_path,
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
024872
+	       addr->sun_path, SECONTEXT_FILE(addr->sun_path),
024872
 	       (int) sizeof(struct sockaddr_un), (int) *plen, SUFFIX_STR, rc);
024872
 
024872
 	memset(addr, 0, sizeof(*addr));
024872
@@ -75,28 +82,34 @@
024872
 			       plen SUFFIX_ARGS);
024872
 	if (rc < 0)
024872
 		perror_msg_and_skip(TEST_SYSCALL_STR);
024872
-	printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%s\"}"
024872
+	printf("%s%s(%d%s%s, {sa_family=AF_UNIX, sun_path=\"%s\"%s}"
024872
 	       ", [%d]%s) = %d\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_S_STR, addr->sun_path,
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
024872
+	       addr->sun_path, SECONTEXT_FILE(addr->sun_path),
024872
 	       (int) *plen, SUFFIX_STR, rc);
024872
 
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr, 0 SUFFIX_ARGS);
024872
-	printf("%s(%d%s, %p, NULL%s) = %s\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr, SUFFIX_STR,
024872
-	       sprintrc(rc));
024872
+	printf("%s%s(%d%s%s, %p, NULL%s) = %s\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_F_STR,
024872
+	       addr, SUFFIX_STR, sprintrc(rc));
024872
 
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, 0, 0 SUFFIX_ARGS);
024872
-	printf("%s(%d%s, NULL, NULL%s) = %s\n",
024872
-	       TEST_SYSCALL_STR, fd, rc == -1 ? PREFIX_F_STR : PREFIX_S_STR,
024872
+	printf("%s%s(%d%s%s, NULL, NULL%s) = %s\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext,
024872
+	       rc == -1 ? PREFIX_F_STR : PREFIX_S_STR,
024872
 	       SUFFIX_STR, sprintrc(rc));
024872
 
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr,
024872
 			       plen + 1 SUFFIX_ARGS);
024872
-	printf("%s(%d%s, %p, %p%s) = %s\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr,
024872
+	printf("%s%s(%d%s%s, %p, %p%s) = %s\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_F_STR, addr,
024872
 	       plen + 1, SUFFIX_STR, sprintrc(rc));
024872
 
024872
 	const size_t offsetof_sun_path = offsetof(struct sockaddr_un, sun_path);
024872
@@ -108,8 +121,9 @@
024872
 			       plen SUFFIX_ARGS);
024872
 	if (rc < 0)
024872
 		perror_msg_and_skip(TEST_SYSCALL_STR);
024872
-	printf("%s(%d%s, {sa_family=AF_UNIX}, [%d => %d]%s) = %d\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_S_STR,
024872
+	printf("%s%s(%d%s%s, {sa_family=AF_UNIX}, [%d => %d]%s) = %d\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
024872
 	       (int) offsetof_sun_path, (int) *plen, SUFFIX_STR, rc);
024872
 
024872
 	++addr;
024872
@@ -121,17 +135,19 @@
024872
 			       plen SUFFIX_ARGS);
024872
 	if (rc < 0)
024872
 		perror_msg_and_skip(TEST_SYSCALL_STR);
024872
-	printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%.*s\"}"
024872
+	printf("%s%s(%d%s%s, {sa_family=AF_UNIX, sun_path=\"%.*s\"%s}"
024872
 	       ", [%d => %d]%s) = %d\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_S_STR,
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
024872
 	       (int) (sizeof(struct sockaddr) - offsetof_sun_path),
024872
-	       addr->sun_path, (int) sizeof(struct sockaddr),
024872
-	       (int) *plen, SUFFIX_STR, rc);
024872
+	       addr->sun_path, SECONTEXT_FILE(addr->sun_path),
024872
+	       (int) sizeof(struct sockaddr), (int) *plen, SUFFIX_STR, rc);
024872
 
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr,
024872
 			       plen SUFFIX_ARGS);
024872
-	printf("%s(%d%s, %p, [%d]%s) = %s\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr,
024872
+	printf("%s%s(%d%s%s, %p, [%d]%s) = %s\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_F_STR, addr,
024872
 	       *plen, SUFFIX_STR, sprintrc(rc));
024872
 }
024872
Index: strace-5.18/tests-mx32/secontext.c
024872
===================================================================
024872
--- strace-5.18.orig/tests-mx32/secontext.c	2022-07-12 18:17:36.747379459 +0200
024872
+++ strace-5.18/tests-mx32/secontext.c	2022-07-12 18:20:18.496470519 +0200
024872
@@ -141,6 +141,21 @@
024872
 	return full_secontext;
024872
 }
024872
 
024872
+static char *
024872
+raw_secontext_full_fd(int fd)
024872
+{
024872
+	int saved_errno = errno;
024872
+	char *full_secontext = NULL;
024872
+	char *secontext;
024872
+
024872
+	if (fgetfilecon(fd, &secontext) >= 0) {
024872
+		full_secontext = strip_trailing_newlines(xstrdup(secontext));
024872
+		freecon(secontext);
024872
+	}
024872
+	errno = saved_errno;
024872
+	return full_secontext;
024872
+}
024872
+
024872
 char *
024872
 get_secontext_field_file(const char *file, enum secontext_field field)
024872
 {
024872
@@ -151,6 +166,16 @@
024872
 	return type;
024872
 }
024872
 
024872
+char *
024872
+get_secontext_field_fd(int fd, enum secontext_field field)
024872
+{
024872
+	char *ctx = raw_secontext_full_fd(fd);
024872
+	char *type =  get_secontext_field(ctx, field);
024872
+	free(ctx);
024872
+
024872
+	return type;
024872
+}
024872
+
024872
 static char *
024872
 raw_secontext_short_file(const char *filename)
024872
 {
024872
@@ -158,6 +183,12 @@
024872
 }
024872
 
024872
 static char *
024872
+raw_secontext_short_fd(int fd)
024872
+{
024872
+	return get_secontext_field_fd(fd, SECONTEXT_TYPE);
024872
+}
024872
+
024872
+static char *
024872
 raw_secontext_full_pid(pid_t pid)
024872
 {
024872
 	int saved_errno = errno;
024872
@@ -205,6 +236,15 @@
024872
 }
024872
 
024872
 char *
024872
+secontext_full_fd(int fd)
024872
+{
024872
+	int saved_errno = errno;
024872
+	char *context = raw_secontext_full_fd(fd);
024872
+	errno = saved_errno;
024872
+	return FORMAT_SPACE_BEFORE(context);
024872
+}
024872
+
024872
+char *
024872
 secontext_full_pid(pid_t pid)
024872
 {
024872
 	return FORMAT_SPACE_AFTER(raw_secontext_full_pid(pid));
024872
@@ -228,6 +268,15 @@
024872
 	errno = saved_errno;
024872
 	return FORMAT_SPACE_BEFORE(context);
024872
 }
024872
+
024872
+char *
024872
+secontext_short_fd(int fd)
024872
+{
024872
+	int saved_errno = errno;
024872
+	char *context = raw_secontext_short_fd(fd);
024872
+	errno = saved_errno;
024872
+	return FORMAT_SPACE_BEFORE(context);
024872
+}
024872
 
024872
 char *
024872
 secontext_short_pid(pid_t pid)
024872
Index: strace-5.18/tests-mx32/secontext.h
024872
===================================================================
024872
--- strace-5.18.orig/tests-mx32/secontext.h	2022-07-12 18:17:36.747379459 +0200
024872
+++ strace-5.18/tests-mx32/secontext.h	2022-07-12 18:20:18.496470519 +0200
024872
@@ -9,9 +9,11 @@
024872
 #include "xmalloc.h"
024872
 #include <unistd.h>
024872
 
024872
+char *secontext_full_fd(int) ATTRIBUTE_MALLOC;
024872
 char *secontext_full_file(const char *, bool) ATTRIBUTE_MALLOC;
024872
 char *secontext_full_pid(pid_t) ATTRIBUTE_MALLOC;
024872
 
024872
+char *secontext_short_fd(int) ATTRIBUTE_MALLOC;
024872
 char *secontext_short_file(const char *, bool) ATTRIBUTE_MALLOC;
024872
 char *secontext_short_pid(pid_t) ATTRIBUTE_MALLOC;
024872
 
024872
@@ -30,6 +32,7 @@
024872
  */
024872
 char *get_secontext_field(const char *full_context, enum secontext_field field);
024872
 
024872
+char *get_secontext_field_fd(int fd, enum secontext_field field);
024872
 char *get_secontext_field_file(const char *file, enum secontext_field field);
024872
 
024872
 void reset_secontext_file(const char *file);
024872
@@ -44,6 +47,7 @@
024872
 #  else
024872
 #   define SECONTEXT_FILE(filename)	secontext_full_file(filename, false)
024872
 #  endif
024872
+#  define SECONTEXT_FD(fd)		secontext_full_fd(fd)
024872
 #  define SECONTEXT_PID(pid)		secontext_full_pid(pid)
024872
 
024872
 # else
024872
@@ -53,6 +57,7 @@
024872
 #  else
024872
 #   define SECONTEXT_FILE(filename)	secontext_short_file(filename, false)
024872
 #  endif
024872
+#  define SECONTEXT_FD(fd)		secontext_short_fd(fd)
024872
 #  define SECONTEXT_PID(pid)		secontext_short_pid(pid)
024872
 
024872
 # endif
024872
@@ -65,6 +70,12 @@
024872
 	return NULL;
024872
 }
024872
 static inline char *
024872
+get_secontext_field_fd(int fd, enum secontext_field field)
024872
+{
024872
+	return NULL;
024872
+}
024872
+
024872
+static inline char *
024872
 get_secontext_field_file(const char *file, enum secontext_field field)
024872
 {
024872
 	return NULL;
024872
@@ -81,6 +92,7 @@
024872
 {
024872
 }
024872
 
024872
+# define SECONTEXT_FD(fd)			xstrdup("")
024872
 # define SECONTEXT_FILE(filename)		xstrdup("")
024872
 # define SECONTEXT_PID(pid)			xstrdup("")
024872
 
024872
Index: strace-5.18/tests-mx32/sockname.c
024872
===================================================================
024872
--- strace-5.18.orig/tests-mx32/sockname.c	2022-07-12 18:17:36.748379448 +0200
024872
+++ strace-5.18/tests-mx32/sockname.c	2022-07-12 18:20:18.496470519 +0200
024872
@@ -18,6 +18,8 @@
024872
 #include <sys/socket.h>
024872
 #include <sys/un.h>
024872
 
024872
+#include "secontext.h"
024872
+
024872
 #ifndef TEST_SYSCALL_NAME
024872
 # error TEST_SYSCALL_NAME must be defined
024872
 #endif
024872
@@ -59,14 +61,19 @@
024872
 	*plen = sizeof(struct sockaddr_un);
024872
 	struct sockaddr_un *addr = tail_alloc(*plen);
024872
 
024872
+	char *my_secontext = SECONTEXT_PID_MY();
024872
+	char *fd_secontext = SECONTEXT_FD(fd);
024872
+
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	int rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, (void *) addr,
024872
 				   plen SUFFIX_ARGS);
024872
 	if (rc < 0)
024872
 		perror_msg_and_skip(TEST_SYSCALL_STR);
024872
-	printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%s\"}"
024872
+	printf("%s%s(%d%s%s, {sa_family=AF_UNIX, sun_path=\"%s\"%s}"
024872
 	       ", [%d => %d]%s) = %d\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_S_STR, addr->sun_path,
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
024872
+	       addr->sun_path, SECONTEXT_FILE(addr->sun_path),
024872
 	       (int) sizeof(struct sockaddr_un), (int) *plen, SUFFIX_STR, rc);
024872
 
024872
 	memset(addr, 0, sizeof(*addr));
024872
@@ -75,28 +82,34 @@
024872
 			       plen SUFFIX_ARGS);
024872
 	if (rc < 0)
024872
 		perror_msg_and_skip(TEST_SYSCALL_STR);
024872
-	printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%s\"}"
024872
+	printf("%s%s(%d%s%s, {sa_family=AF_UNIX, sun_path=\"%s\"%s}"
024872
 	       ", [%d]%s) = %d\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_S_STR, addr->sun_path,
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
024872
+	       addr->sun_path, SECONTEXT_FILE(addr->sun_path),
024872
 	       (int) *plen, SUFFIX_STR, rc);
024872
 
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr, 0 SUFFIX_ARGS);
024872
-	printf("%s(%d%s, %p, NULL%s) = %s\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr, SUFFIX_STR,
024872
-	       sprintrc(rc));
024872
+	printf("%s%s(%d%s%s, %p, NULL%s) = %s\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_F_STR,
024872
+	       addr, SUFFIX_STR, sprintrc(rc));
024872
 
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	rc = TEST_SYSCALL_NAME(fd PREFIX_S_ARGS, 0, 0 SUFFIX_ARGS);
024872
-	printf("%s(%d%s, NULL, NULL%s) = %s\n",
024872
-	       TEST_SYSCALL_STR, fd, rc == -1 ? PREFIX_F_STR : PREFIX_S_STR,
024872
+	printf("%s%s(%d%s%s, NULL, NULL%s) = %s\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext,
024872
+	       rc == -1 ? PREFIX_F_STR : PREFIX_S_STR,
024872
 	       SUFFIX_STR, sprintrc(rc));
024872
 
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr,
024872
 			       plen + 1 SUFFIX_ARGS);
024872
-	printf("%s(%d%s, %p, %p%s) = %s\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr,
024872
+	printf("%s%s(%d%s%s, %p, %p%s) = %s\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_F_STR, addr,
024872
 	       plen + 1, SUFFIX_STR, sprintrc(rc));
024872
 
024872
 	const size_t offsetof_sun_path = offsetof(struct sockaddr_un, sun_path);
024872
@@ -108,8 +121,9 @@
024872
 			       plen SUFFIX_ARGS);
024872
 	if (rc < 0)
024872
 		perror_msg_and_skip(TEST_SYSCALL_STR);
024872
-	printf("%s(%d%s, {sa_family=AF_UNIX}, [%d => %d]%s) = %d\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_S_STR,
024872
+	printf("%s%s(%d%s%s, {sa_family=AF_UNIX}, [%d => %d]%s) = %d\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
024872
 	       (int) offsetof_sun_path, (int) *plen, SUFFIX_STR, rc);
024872
 
024872
 	++addr;
024872
@@ -121,17 +135,19 @@
024872
 			       plen SUFFIX_ARGS);
024872
 	if (rc < 0)
024872
 		perror_msg_and_skip(TEST_SYSCALL_STR);
024872
-	printf("%s(%d%s, {sa_family=AF_UNIX, sun_path=\"%.*s\"}"
024872
+	printf("%s%s(%d%s%s, {sa_family=AF_UNIX, sun_path=\"%.*s\"%s}"
024872
 	       ", [%d => %d]%s) = %d\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_S_STR,
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_S_STR,
024872
 	       (int) (sizeof(struct sockaddr) - offsetof_sun_path),
024872
-	       addr->sun_path, (int) sizeof(struct sockaddr),
024872
-	       (int) *plen, SUFFIX_STR, rc);
024872
+	       addr->sun_path, SECONTEXT_FILE(addr->sun_path),
024872
+	       (int) sizeof(struct sockaddr), (int) *plen, SUFFIX_STR, rc);
024872
 
024872
 	PREPARE_TEST_SYSCALL_INVOCATION;
024872
 	rc = TEST_SYSCALL_NAME(fd PREFIX_F_ARGS, (void *) addr,
024872
 			       plen SUFFIX_ARGS);
024872
-	printf("%s(%d%s, %p, [%d]%s) = %s\n",
024872
-	       TEST_SYSCALL_STR, fd, PREFIX_F_STR, addr,
024872
+	printf("%s%s(%d%s%s, %p, [%d]%s) = %s\n",
024872
+	       my_secontext,
024872
+	       TEST_SYSCALL_STR, fd, fd_secontext, PREFIX_F_STR, addr,
024872
 	       *plen, SUFFIX_STR, sprintrc(rc));
024872
 }