@@ -1077,6 +1077,33 @@ send_sync_cb (void *data, libcrun_error_t *err)
1077
1077
return sync_socket_wait_sync (NULL , sync_socket_fd , false, err );
1078
1078
}
1079
1079
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
+
1080
1107
/* Initialize the environment where the container process runs.
1081
1108
It is used by the container init process. */
1082
1109
static int
@@ -1184,6 +1211,10 @@ container_init_setup (void *args, pid_t own_pid, char *notify_socket, int sync_s
1184
1211
if (UNLIKELY (ret < 0 ))
1185
1212
return ret ;
1186
1213
1214
+ ret = maybe_chown_std_streams (container -> container_uid , container -> container_gid , err );
1215
+ if (UNLIKELY (ret < 0 ))
1216
+ return ret ;
1217
+
1187
1218
if (clearenv ())
1188
1219
return crun_make_error (err , errno , "clearenv" );
1189
1220
@@ -2200,8 +2231,8 @@ get_seccomp_receiver_fd (libcrun_container_t *container, int *fd, int *self_rece
2200
2231
}
2201
2232
2202
2233
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 )
2205
2236
{
2206
2237
runtime_spec_schema_config_schema * def = container -> container_def ;
2207
2238
int ret ;
@@ -3335,6 +3366,10 @@ libcrun_container_exec_with_options (libcrun_context_t *context, const char *id,
3335
3366
if (UNLIKELY (ret < 0 ))
3336
3367
return ret ;
3337
3368
3369
+ ret = maybe_chown_std_streams (container_uid , container_gid , err );
3370
+ if (UNLIKELY (ret < 0 ))
3371
+ return ret ;
3372
+
3338
3373
if (process -> capabilities )
3339
3374
capabilities = process -> capabilities ;
3340
3375
else if (container -> container_def -> process )
0 commit comments