Skip to content

Commit ddef3ec

Browse files
keszybzYHNdnzj
authored andcommitted
run: do not pass the pty slave fd to transient service in a machine
Follow-up for 28459ba The pty path returned by OpenMachinePTY() cannot be opened from outside the machine, hence let's use the plain Standard{Input,Output,Error}=tty in such a case. This means if --machine= is specified, #32916 would occur. A comprehensive fix requires a new dbus method in machined, which shall be material for v257. See also: systemd/systemd#33216 (comment) Replaces #33216 Co-authored-by: Mike Yuan <[email protected]>
1 parent 7632b8a commit ddef3ec

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/run/run.c

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ static int transient_kill_set_properties(sd_bus_message *m) {
10731073
return 0;
10741074
}
10751075

1076-
static int transient_service_set_properties(sd_bus_message *m, const char *pty_path) {
1076+
static int transient_service_set_properties(sd_bus_message *m, const char *pty_path, int pty_fd) {
10771077
bool send_term = false;
10781078
int r;
10791079

@@ -1083,6 +1083,7 @@ static int transient_service_set_properties(sd_bus_message *m, const char *pty_p
10831083
bool use_ex_prop = arg_expand_environment == 0;
10841084

10851085
assert(m);
1086+
assert(pty_path || pty_fd < 0);
10861087

10871088
r = transient_unit_set_properties(m, UNIT_SERVICE, arg_property);
10881089
if (r < 0)
@@ -1133,18 +1134,22 @@ static int transient_service_set_properties(sd_bus_message *m, const char *pty_p
11331134
}
11341135

11351136
if (pty_path) {
1136-
_cleanup_close_ int pty_slave = -EBADF;
1137-
1138-
pty_slave = open_terminal(pty_path, O_RDWR|O_NOCTTY|O_CLOEXEC);
1139-
if (pty_slave < 0)
1140-
return pty_slave;
1137+
r = sd_bus_message_append(m, "(sv)", "TTYPath", "s", pty_path);
1138+
if (r < 0)
1139+
return bus_log_create_error(r);
11411140

1142-
r = sd_bus_message_append(m,
1143-
"(sv)(sv)(sv)(sv)",
1144-
"StandardInputFileDescriptor", "h", pty_slave,
1145-
"StandardOutputFileDescriptor", "h", pty_slave,
1146-
"StandardErrorFileDescriptor", "h", pty_slave,
1147-
"TTYPath", "s", pty_path);
1141+
if (pty_fd >= 0)
1142+
r = sd_bus_message_append(m,
1143+
"(sv)(sv)(sv)",
1144+
"StandardInputFileDescriptor", "h", pty_fd,
1145+
"StandardOutputFileDescriptor", "h", pty_fd,
1146+
"StandardErrorFileDescriptor", "h", pty_fd);
1147+
else
1148+
r = sd_bus_message_append(m,
1149+
"(sv)(sv)(sv)",
1150+
"StandardInput", "s", "tty",
1151+
"StandardOutput", "s", "tty",
1152+
"StandardError", "s", "tty");
11481153
if (r < 0)
11491154
return bus_log_create_error(r);
11501155

@@ -1526,7 +1531,8 @@ static int make_transient_service_unit(
15261531
sd_bus *bus,
15271532
sd_bus_message **message,
15281533
const char *service,
1529-
const char *pty_path) {
1534+
const char *pty_path,
1535+
int pty_fd) {
15301536

15311537
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
15321538
int r;
@@ -1553,7 +1559,7 @@ static int make_transient_service_unit(
15531559
if (r < 0)
15541560
return bus_log_create_error(r);
15551561

1556-
r = transient_service_set_properties(m, pty_path);
1562+
r = transient_service_set_properties(m, pty_path, pty_fd);
15571563
if (r < 0)
15581564
return r;
15591565

@@ -1675,7 +1681,7 @@ static int start_transient_service(sd_bus *bus) {
16751681
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
16761682
_cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
16771683
_cleanup_free_ char *service = NULL, *pty_path = NULL;
1678-
_cleanup_close_ int master = -EBADF;
1684+
_cleanup_close_ int master = -EBADF, slave = -EBADF;
16791685
int r;
16801686

16811687
assert(bus);
@@ -1702,6 +1708,10 @@ static int start_transient_service(sd_bus *bus) {
17021708
if (unlockpt(master) < 0)
17031709
return log_error_errno(errno, "Failed to unlock tty: %m");
17041710

1711+
slave = open_terminal(pty_path, O_RDWR|O_NOCTTY|O_CLOEXEC);
1712+
if (slave < 0)
1713+
return log_error_errno(slave, "Failed to open pty slave: %m");
1714+
17051715
} else if (arg_transport == BUS_TRANSPORT_MACHINE) {
17061716
_cleanup_(sd_bus_unrefp) sd_bus *system_bus = NULL;
17071717
_cleanup_(sd_bus_message_unrefp) sd_bus_message *pty_reply = NULL;
@@ -1731,6 +1741,9 @@ static int start_transient_service(sd_bus *bus) {
17311741
pty_path = strdup(s);
17321742
if (!pty_path)
17331743
return log_oom();
1744+
1745+
// FIXME: Introduce OpenMachinePTYEx() that accepts ownership/permission as param
1746+
// and additionally returns the pty fd, for #33216 and #32999
17341747
} else
17351748
assert_not_reached();
17361749
}
@@ -1757,9 +1770,10 @@ static int start_transient_service(sd_bus *bus) {
17571770
return r;
17581771
}
17591772

1760-
r = make_transient_service_unit(bus, &m, service, pty_path);
1773+
r = make_transient_service_unit(bus, &m, service, pty_path, slave);
17611774
if (r < 0)
17621775
return r;
1776+
slave = safe_close(slave);
17631777

17641778
polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
17651779

@@ -2204,7 +2218,7 @@ static int make_transient_trigger_unit(
22042218
if (r < 0)
22052219
return bus_log_create_error(r);
22062220

2207-
r = transient_service_set_properties(m, NULL);
2221+
r = transient_service_set_properties(m, /* pty_path = */ NULL, /* pty_fd = */ -EBADF);
22082222
if (r < 0)
22092223
return r;
22102224

0 commit comments

Comments
 (0)