public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob 64b926bdf409d9b4850793dc45f28a99e3fda0ab 4215 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
 
// SPDX-License-Identifier: GPL-2.0-or-later

/* PASST - Plug A Simple Socket Transport
 *  for qemu/UNIX domain socket mode
 *
 * PASTA - Pack A Subtle Tap Abstraction
 *  for network namespace/tap device mode
 *
 * passt-repair.c - Privileged helper to set/clear TCP_REPAIR on sockets
 *
 * Copyright (c) 2025 Red Hat GmbH
 * Author: Stefano Brivio <sbrivio@redhat.com>
 *
 * Connect to passt via UNIX domain socket, receive sockets via SCM_RIGHTS along
 * with byte commands mapping to TCP_REPAIR values, and switch repair mode on or
 * off. Reply by echoing the command. Exit on EOF.
 */

#include <sys/prctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <netdb.h>

#include <netinet/tcp.h>

#include <linux/audit.h>
#include <linux/capability.h>
#include <linux/filter.h>
#include <linux/seccomp.h>

#include "seccomp_repair.h"

#define SCM_MAX_FD 253 /* From Linux kernel (include/net/scm.h), not in UAPI */

#define die(status, ...)			\
	do {					\
		fprintf(stderr, __VA_ARGS__);	\
		fprintf(stderr, "\n");		\
		_exit(status);			\
	} while (0)

#define die_errno(...)					\
	do {						\
		int err_ = errno;			\
		fprintf(stderr, __VA_ARGS__);		\
		fprintf(stderr, ": %d\n", err_);	\
		_exit(1);				\
	} while (0)

/**
 * main() - Entry point and whole program with loop
 * @argc:	Argument count, must be 2
 * @argv:	Argument: path of UNIX domain socket to connect to
 *
 * Return: 0 on success (EOF), 1 on error, 2 on usage error
 *
 * #syscalls:repair connect setsockopt write close exit_group
 * #syscalls:repair socket s390x:socketcall i686:socketcall
 * #syscalls:repair recvfrom recvmsg arm:recv ppc64le:recv
 * #syscalls:repair sendto sendmsg arm:send ppc64le:send
 */
int main(int argc, char **argv)
{
	char buf[CMSG_SPACE(sizeof(int) * SCM_MAX_FD)]
	     __attribute__ ((aligned(__alignof__(struct cmsghdr))));
	struct sockaddr_un a = { AF_UNIX, "" };
	int fds[SCM_MAX_FD], s, ret, i, n = 0;
	struct sock_fprog prog;
	int8_t cmd = INT8_MAX;
	struct cmsghdr *cmsg;
	struct msghdr msg;
	struct iovec iov;
	size_t fdlen;
	int op;

	prctl(PR_SET_DUMPABLE, 0);

	prog.len = (unsigned short)sizeof(filter_repair) /
				   sizeof(filter_repair[0]);
	prog.filter = filter_repair;
	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) ||
	    prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog))
		die_errno("Failed to apply seccomp filter");

	iov = (struct iovec){ &cmd, sizeof(cmd) };
	msg = (struct msghdr){ .msg_name = NULL, .msg_namelen = 0,
			       .msg_iov = &iov, .msg_iovlen = 1,
			       .msg_control = buf,
			       .msg_controllen = sizeof(buf),
			       .msg_flags = 0 };
	cmsg = CMSG_FIRSTHDR(&msg);

	if (argc != 2)
		die(2, "Usage: %s PATH", argv[0]);

	ret = snprintf(a.sun_path, sizeof(a.sun_path), "%s", argv[1]);
	if (ret <= 0 || ret >= (int)sizeof(a.sun_path))
		die(2, "Invalid socket path: %s", argv[1]);

	if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
		die_errno("Failed to create AF_UNIX socket");

	if (connect(s, (struct sockaddr *)&a, sizeof(a)))
		die_errno("Failed to connect to %s", argv[1]);

loop:
	ret = recvmsg(s, &msg, 0);
	if (ret < 0) {
		if (errno == ECONNRESET)
			ret = 0;
		else
			die_errno("Failed to read message");
	}

	if (!ret)	/* Done */
		_exit(0);

	if (!cmsg || cmsg->cmsg_type != SCM_RIGHTS)
		die(1, "No/bad ancillary data from peer");

	fdlen = ((char *)cmsg + cmsg->cmsg_len) - (char *)CMSG_DATA(cmsg);
	if (fdlen % sizeof(int) != 0 || fdlen > sizeof(fds))
		die(1, "Invalid SCM_RIGHTS payload length %zu from peer", fdlen);
	n = fdlen / sizeof(int);

	memcpy(fds, CMSG_DATA(cmsg), fdlen);

	if (cmd != TCP_REPAIR_ON && cmd != TCP_REPAIR_OFF &&
	    cmd != TCP_REPAIR_OFF_NO_WP)
		die(1, "Unsupported command 0x%04x", cmd);

	op = cmd;

	for (i = 0; i < n; i++) {
		if (setsockopt(fds[i], SOL_TCP, TCP_REPAIR, &op, sizeof(op))) {
			die_errno("Setting TCP_REPAIR to %i on socket %i",
				   op, fds[i]);
		}

		/* Close _our_ copy */
		close(fds[i]);
	}

	/* Confirm setting by echoing the command back */
	if (send(s, &cmd, sizeof(cmd), 0) < 0)
		die_errno("Reply to %i", op);

	goto loop;

	return 0;
}

debug log:

solving 64b926bd ...
found 64b926bd in https://archives.passt.top/passt-dev/20250221065010.3681262-4-david@gibson.dropbear.id.au/
found 3c358e27 in https://archives.passt.top/passt-dev/20250221065010.3681262-3-david@gibson.dropbear.id.au/
found d785cd16 in https://archives.passt.top/passt-dev/20250221065010.3681262-2-david@gibson.dropbear.id.au/
found e0c366e5 in https://passt.top/passt
preparing index
index prepared:
100644 e0c366e566f3e79f8420be74550803d41c8426dd	passt-repair.c

applying [1/3] https://archives.passt.top/passt-dev/20250221065010.3681262-2-david@gibson.dropbear.id.au/
diff --git a/passt-repair.c b/passt-repair.c
index e0c366e5..d785cd16 100644


applying [2/3] https://archives.passt.top/passt-dev/20250221065010.3681262-3-david@gibson.dropbear.id.au/
diff --git a/passt-repair.c b/passt-repair.c
index d785cd16..3c358e27 100644


applying [3/3] https://archives.passt.top/passt-dev/20250221065010.3681262-4-david@gibson.dropbear.id.au/
diff --git a/passt-repair.c b/passt-repair.c
index 3c358e27..64b926bd 100644

Checking patch passt-repair.c...
Applied patch passt-repair.c cleanly.
Checking patch passt-repair.c...
Applied patch passt-repair.c cleanly.
Checking patch passt-repair.c...
Applied patch passt-repair.c cleanly.

index at:
100644 64b926bdf409d9b4850793dc45f28a99e3fda0ab	passt-repair.c

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).