Skip to content

Commit daab5ae

Browse files
authored
Merge pull request #755 from giuseppe/chown-std-streams
crun: chown std streams
2 parents 70db6c6 + cab3d52 commit daab5ae

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/libcrun/container.c

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,33 @@ send_sync_cb (void *data, libcrun_error_t *err)
10771077
return sync_socket_wait_sync (NULL, sync_socket_fd, false, err);
10781078
}
10791079

1080+
static int
1081+
maybe_chown_std_streams (uid_t container_uid, gid_t container_gid,
1082+
libcrun_error_t *err)
1083+
{
1084+
int ret, i;
1085+
1086+
for (i = 0; i < 3; i++)
1087+
{
1088+
if (! isatty (i))
1089+
{
1090+
ret = fchown (i, container_uid, container_gid);
1091+
if (UNLIKELY (ret < 0))
1092+
{
1093+
/* EINVAL means the user is not mapped in the current userns.
1094+
Ignore EPERM as well as there is no reason to fail so early, and
1095+
let the container payload deal with it.
1096+
*/
1097+
if (errno == EINVAL || errno == EPERM)
1098+
continue;
1099+
1100+
return crun_make_error (err, errno, "fchown std stream %i", i);
1101+
}
1102+
}
1103+
}
1104+
return 0;
1105+
}
1106+
10801107
/* Initialize the environment where the container process runs.
10811108
It is used by the container init process. */
10821109
static int
@@ -1184,6 +1211,10 @@ container_init_setup (void *args, pid_t own_pid, char *notify_socket, int sync_s
11841211
if (UNLIKELY (ret < 0))
11851212
return ret;
11861213

1214+
ret = maybe_chown_std_streams (container->container_uid, container->container_gid, err);
1215+
if (UNLIKELY (ret < 0))
1216+
return ret;
1217+
11871218
if (clearenv ())
11881219
return crun_make_error (err, errno, "clearenv");
11891220

@@ -2200,8 +2231,8 @@ get_seccomp_receiver_fd (libcrun_container_t *container, int *fd, int *self_rece
22002231
}
22012232

22022233
static int
2203-
libcrun_container_run_internal (libcrun_container_t *container, libcrun_context_t *context, int container_ready_fd,
2204-
libcrun_error_t *err)
2234+
libcrun_container_run_internal (libcrun_container_t *container, libcrun_context_t *context,
2235+
int container_ready_fd, libcrun_error_t *err)
22052236
{
22062237
runtime_spec_schema_config_schema *def = container->container_def;
22072238
int ret;
@@ -3335,6 +3366,10 @@ libcrun_container_exec_with_options (libcrun_context_t *context, const char *id,
33353366
if (UNLIKELY (ret < 0))
33363367
return ret;
33373368

3369+
ret = maybe_chown_std_streams (container_uid, container_gid, err);
3370+
if (UNLIKELY (ret < 0))
3371+
return ret;
3372+
33383373
if (process->capabilities)
33393374
capabilities = process->capabilities;
33403375
else if (container->container_def->process)

0 commit comments

Comments
 (0)