-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
attach.c
1507 lines (1226 loc) · 38.2 KB
/
attach.c
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: LGPL-2.1+ */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <linux/unistd.h>
#include <pwd.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <termios.h>
#include <unistd.h>
#include <lxc/lxccontainer.h>
#include "af_unix.h"
#include "attach.h"
#include "caps.h"
#include "cgroup.h"
#include "commands.h"
#include "conf.h"
#include "config.h"
#include "confile.h"
#include "log.h"
#include "lsm/lsm.h"
#include "lxclock.h"
#include "lxcseccomp.h"
#include "macro.h"
#include "mainloop.h"
#include "memory_utils.h"
#include "namespace.h"
#include "process_utils.h"
#include "syscall_wrappers.h"
#include "terminal.h"
#include "utils.h"
#if HAVE_SYS_PERSONALITY_H
#include <sys/personality.h>
#endif
lxc_log_define(attach, lxc);
/* Define default options if no options are supplied by the user. */
static lxc_attach_options_t attach_static_default_options = LXC_ATTACH_OPTIONS_DEFAULT;
static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
{
__do_free char *line = NULL;
__do_fclose FILE *proc_file = NULL;
__do_free struct lxc_proc_context_info *info = NULL;
int ret;
bool found;
char proc_fn[LXC_PROC_STATUS_LEN];
size_t line_bufsz = 0;
/* Read capabilities. */
ret = snprintf(proc_fn, LXC_PROC_STATUS_LEN, "/proc/%d/status", pid);
if (ret < 0 || ret >= LXC_PROC_STATUS_LEN)
return NULL;
proc_file = fopen(proc_fn, "re");
if (!proc_file)
return log_error_errno(NULL, errno, "Failed to open %s", proc_fn);
info = calloc(1, sizeof(*info));
if (!info)
return NULL;
found = false;
while (getline(&line, &line_bufsz, proc_file) != -1) {
ret = sscanf(line, "CapBnd: %llx", &info->capability_mask);
if (ret != EOF && ret == 1) {
found = true;
break;
}
}
if (!found)
return log_error_errno(NULL, ENOENT, "Failed to read capability bounding set from %s", proc_fn);
info->lsm_label = lsm_process_label_get(pid);
info->ns_inherited = 0;
for (int i = 0; i < LXC_NS_MAX; i++)
info->ns_fd[i] = -EBADF;
return move_ptr(info);
}
static inline void lxc_proc_close_ns_fd(struct lxc_proc_context_info *ctx)
{
for (int i = 0; i < LXC_NS_MAX; i++)
close_prot_errno_disarm(ctx->ns_fd[i]);
}
static void lxc_proc_put_context_info(struct lxc_proc_context_info *ctx)
{
free(ctx->lsm_label);
ctx->lsm_label = NULL;
if (ctx->container) {
lxc_container_put(ctx->container);
ctx->container = NULL;
}
lxc_proc_close_ns_fd(ctx);
free(ctx);
}
/**
* in_same_namespace - Check whether two processes are in the same namespace.
* @pid1 - PID of the first process.
* @pid2 - PID of the second process.
* @ns - Name of the namespace to check. Must correspond to one of the names
* for the namespaces as shown in /proc/<pid/ns/
*
* If the two processes are not in the same namespace returns an fd to the
* namespace of the second process identified by @pid2. If the two processes are
* in the same namespace returns -EINVAL, -1 if an error occurred.
*/
static int in_same_namespace(pid_t pid1, pid_t pid2, const char *ns)
{
__do_close int ns_fd1 = -EBADF, ns_fd2 = -EBADF;
int ret = -1;
struct stat ns_st1, ns_st2;
ns_fd1 = lxc_preserve_ns(pid1, ns);
if (ns_fd1 < 0) {
/* The kernel does not support this namespace. This is not an
* error.
*/
if (errno == ENOENT)
return -EINVAL;
return -1;
}
ns_fd2 = lxc_preserve_ns(pid2, ns);
if (ns_fd2 < 0)
return -1;
ret = fstat(ns_fd1, &ns_st1);
if (ret < 0)
return -1;
ret = fstat(ns_fd2, &ns_st2);
if (ret < 0)
return -1;
/* processes are in the same namespace */
if ((ns_st1.st_dev == ns_st2.st_dev) && (ns_st1.st_ino == ns_st2.st_ino))
return -EINVAL;
/* processes are in different namespaces */
return move_fd(ns_fd2);
}
static int lxc_attach_to_ns(pid_t pid, struct lxc_proc_context_info *ctx)
{
for (int i = 0; i < LXC_NS_MAX; i++) {
int ret;
if (ctx->ns_fd[i] < 0)
continue;
ret = setns(ctx->ns_fd[i], ns_info[i].clone_flag);
if (ret < 0)
return log_error_errno(-1,
errno, "Failed to attach to %s namespace of %d",
ns_info[i].proc_name, pid);
DEBUG("Attached to %s namespace of %d", ns_info[i].proc_name, pid);
}
return 0;
}
int lxc_attach_remount_sys_proc(void)
{
int ret;
ret = unshare(CLONE_NEWNS);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to unshare mount namespace");
if (detect_shared_rootfs() && mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL))
SYSERROR("Failed to recursively turn root mount tree into dependent mount. Continuing...");
/* Assume /proc is always mounted, so remount it. */
ret = umount2("/proc", MNT_DETACH);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to unmount /proc");
ret = mount("none", "/proc", "proc", 0, NULL);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to remount /proc");
/*
* Try to umount /sys. If it's not a mount point, we'll get EINVAL, then
* we ignore it because it may not have been mounted in the first place.
*/
ret = umount2("/sys", MNT_DETACH);
if (ret < 0 && errno != EINVAL)
return log_error_errno(-1, errno, "Failed to unmount /sys");
/* Remount it. */
if (ret == 0 && mount("none", "/sys", "sysfs", 0, NULL))
return log_error_errno(-1, errno, "Failed to remount /sys");
return 0;
}
static int lxc_attach_drop_privs(struct lxc_proc_context_info *ctx)
{
int last_cap;
last_cap = lxc_caps_last_cap();
for (int cap = 0; cap <= last_cap; cap++) {
if (ctx->capability_mask & (1LL << cap))
continue;
if (prctl(PR_CAPBSET_DROP, prctl_arg(cap), prctl_arg(0),
prctl_arg(0), prctl_arg(0)))
return log_error_errno(-1, errno, "Failed to drop capability %d", cap);
TRACE("Dropped capability %d", cap);
}
return 0;
}
static int lxc_attach_set_environment(struct lxc_proc_context_info *init_ctx,
enum lxc_attach_env_policy_t policy,
char **extra_env, char **extra_keep)
{
int ret;
struct lxc_list *iterator;
if (policy == LXC_ATTACH_CLEAR_ENV) {
int path_kept = 0;
char **extra_keep_store = NULL;
if (extra_keep) {
size_t count, i;
for (count = 0; extra_keep[count]; count++)
;
extra_keep_store = calloc(count, sizeof(char *));
if (!extra_keep_store)
return -1;
for (i = 0; i < count; i++) {
char *v = getenv(extra_keep[i]);
if (v) {
extra_keep_store[i] = strdup(v);
if (!extra_keep_store[i]) {
while (i > 0)
free(extra_keep_store[--i]);
free(extra_keep_store);
return -1;
}
if (strcmp(extra_keep[i], "PATH") == 0)
path_kept = 1;
}
}
}
if (clearenv()) {
if (extra_keep_store) {
char **p;
for (p = extra_keep_store; *p; p++)
free(*p);
free(extra_keep_store);
}
return log_error(-1, "Failed to clear environment");
}
if (extra_keep_store) {
size_t i;
for (i = 0; extra_keep[i]; i++) {
if (extra_keep_store[i]) {
ret = setenv(extra_keep[i], extra_keep_store[i], 1);
if (ret < 0)
SYSWARN("Failed to set environment variable");
}
free(extra_keep_store[i]);
}
free(extra_keep_store);
}
/* Always set a default path; shells and execlp tend to be fine
* without it, but there is a disturbing number of C programs
* out there that just assume that getenv("PATH") is never NULL
* and then die a painful segfault death.
*/
if (!path_kept) {
ret = setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 1);
if (ret < 0)
SYSWARN("Failed to set environment variable");
}
}
ret = putenv("container=lxc");
if (ret < 0)
return log_warn(-1, "Failed to set environment variable");
/* Set container environment variables.*/
if (init_ctx && init_ctx->container && init_ctx->container->lxc_conf) {
lxc_list_for_each(iterator, &init_ctx->container->lxc_conf->environment) {
char *env_tmp;
env_tmp = strdup((char *)iterator->elem);
if (!env_tmp)
return -1;
ret = putenv(env_tmp);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to set environment variable: %s", (char *)iterator->elem);
}
}
/* Set extra environment variables. */
if (extra_env) {
for (; *extra_env; extra_env++) {
char *p;
/* We just assume the user knows what they are doing, so
* we don't do any checks.
*/
p = strdup(*extra_env);
if (!p)
return -1;
ret = putenv(p);
if (ret < 0)
SYSWARN("Failed to set environment variable");
}
}
return 0;
}
static char *lxc_attach_getpwshell(uid_t uid)
{
__do_free char *line = NULL, *result = NULL;
__do_fclose FILE *pipe_f = NULL;
int fd, ret;
pid_t pid;
int pipes[2];
bool found = false;
size_t line_bufsz = 0;
/* We need to fork off a process that runs the getent program, and we
* need to capture its output, so we use a pipe for that purpose.
*/
ret = pipe2(pipes, O_CLOEXEC);
if (ret < 0)
return NULL;
pid = fork();
if (pid < 0) {
close(pipes[0]);
close(pipes[1]);
return NULL;
}
if (!pid) {
char uid_buf[32];
char *arguments[] = {
"getent",
"passwd",
uid_buf,
NULL
};
close(pipes[0]);
/* We want to capture stdout. */
ret = dup2(pipes[1], STDOUT_FILENO);
close(pipes[1]);
if (ret < 0)
_exit(EXIT_FAILURE);
/* Get rid of stdin/stderr, so we try to associate it with
* /dev/null.
*/
fd = open_devnull();
if (fd < 0) {
close(STDIN_FILENO);
close(STDERR_FILENO);
} else {
(void)dup3(fd, STDIN_FILENO, O_CLOEXEC);
(void)dup3(fd, STDERR_FILENO, O_CLOEXEC);
close(fd);
}
/* Finish argument list. */
ret = snprintf(uid_buf, sizeof(uid_buf), "%ld", (long)uid);
if (ret <= 0 || ret >= sizeof(uid_buf))
_exit(EXIT_FAILURE);
/* Try to run getent program. */
(void)execvp("getent", arguments);
_exit(EXIT_FAILURE);
}
close(pipes[1]);
pipe_f = fdopen(pipes[0], "re");
if (!pipe_f) {
close(pipes[0]);
goto reap_child;
}
/* Transfer ownership of pipes[0] to pipe_f. */
move_fd(pipes[0]);
while (getline(&line, &line_bufsz, pipe_f) != -1) {
int i;
long value;
char *token;
char *endptr = NULL, *saveptr = NULL;
/* If we already found something, just continue to read
* until the pipe doesn't deliver any more data, but
* don't modify the existing data structure.
*/
if (found)
continue;
if (!line)
continue;
/* Trim line on the right hand side. */
for (i = strlen(line); i > 0 && (line[i - 1] == '\n' || line[i - 1] == '\r'); --i)
line[i - 1] = '\0';
/* Split into tokens: first: user name. */
token = strtok_r(line, ":", &saveptr);
if (!token)
continue;
/* next: dummy password field */
token = strtok_r(NULL, ":", &saveptr);
if (!token)
continue;
/* next: user id */
token = strtok_r(NULL, ":", &saveptr);
value = token ? strtol(token, &endptr, 10) : 0;
if (!token || !endptr || *endptr || value == LONG_MIN ||
value == LONG_MAX)
continue;
/* dummy sanity check: user id matches */
if ((uid_t)value != uid)
continue;
/* skip fields: gid, gecos, dir, go to next field 'shell' */
for (i = 0; i < 4; i++) {
token = strtok_r(NULL, ":", &saveptr);
if (!token)
continue;
}
if (!token)
continue;
free_disarm(result);
result = strdup(token);
/* Sanity check that there are no fields after that. */
token = strtok_r(NULL, ":", &saveptr);
if (token)
continue;
found = true;
}
reap_child:
ret = wait_for_pid(pid);
if (ret < 0)
return NULL;
if (!found)
return NULL;
return move_ptr(result);
}
static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid)
{
__do_free char *line = NULL;
__do_fclose FILE *proc_file = NULL;
char proc_fn[LXC_PROC_STATUS_LEN];
int ret;
size_t line_bufsz = 0;
long value = -1;
uid_t uid = LXC_INVALID_UID;
gid_t gid = LXC_INVALID_GID;
ret = snprintf(proc_fn, LXC_PROC_STATUS_LEN, "/proc/%d/status", 1);
if (ret < 0 || ret >= LXC_PROC_STATUS_LEN)
return;
proc_file = fopen(proc_fn, "re");
if (!proc_file)
return;
while (getline(&line, &line_bufsz, proc_file) != -1) {
/* Format is: real, effective, saved set user, fs we only care
* about real uid.
*/
ret = sscanf(line, "Uid: %ld", &value);
if (ret != EOF && ret == 1) {
uid = (uid_t)value;
} else {
ret = sscanf(line, "Gid: %ld", &value);
if (ret != EOF && ret == 1)
gid = (gid_t)value;
}
if (uid != LXC_INVALID_UID && gid != LXC_INVALID_GID)
break;
}
/* Only override arguments if we found something. */
if (uid != LXC_INVALID_UID)
*init_uid = uid;
if (gid != LXC_INVALID_GID)
*init_gid = gid;
/* TODO: we should also parse supplementary groups and use
* setgroups() to set them.
*/
}
static bool fetch_seccomp(struct lxc_container *c, lxc_attach_options_t *options)
{
__do_free char *path = NULL;
int ret;
bool bret;
if (!(options->namespaces & CLONE_NEWNS) ||
!(options->attach_flags & LXC_ATTACH_LSM)) {
free_disarm(c->lxc_conf->seccomp.seccomp);
return true;
}
/* Remove current setting. */
if (!c->set_config_item(c, "lxc.seccomp.profile", "") &&
!c->set_config_item(c, "lxc.seccomp", ""))
return false;
/* Fetch the current profile path over the cmd interface. */
path = c->get_running_config_item(c, "lxc.seccomp.profile");
if (!path) {
INFO("Failed to retrieve lxc.seccomp.profile");
path = c->get_running_config_item(c, "lxc.seccomp");
if (!path)
return log_info(true, "Failed to retrieve lxc.seccomp");
}
/* Copy the value into the new lxc_conf. */
bret = c->set_config_item(c, "lxc.seccomp.profile", path);
if (!bret)
return false;
/* Attempt to parse the resulting config. */
ret = lxc_read_seccomp_config(c->lxc_conf);
if (ret < 0)
return log_error(false, "Failed to retrieve seccomp policy");
return log_info(true, "Retrieved seccomp policy");
}
static bool no_new_privs(struct lxc_container *c, lxc_attach_options_t *options)
{
__do_free char *val = NULL;
/* Remove current setting. */
if (!c->set_config_item(c, "lxc.no_new_privs", ""))
return log_info(false, "Failed to unset lxc.no_new_privs");
/* Retrieve currently active setting. */
val = c->get_running_config_item(c, "lxc.no_new_privs");
if (!val)
return log_info(false, "Failed to retrieve lxc.no_new_privs");
/* Set currently active setting. */
return c->set_config_item(c, "lxc.no_new_privs", val);
}
static signed long get_personality(const char *name, const char *lxcpath)
{
__do_free char *p = NULL;
p = lxc_cmd_get_config_item(name, "lxc.arch", lxcpath);
if (!p)
return -1;
return lxc_config_parse_arch(p);
}
struct attach_clone_payload {
int ipc_socket;
int terminal_pts_fd;
lxc_attach_options_t *options;
struct lxc_proc_context_info *init_ctx;
lxc_attach_exec_t exec_function;
void *exec_payload;
};
static void lxc_put_attach_clone_payload(struct attach_clone_payload *p)
{
close_prot_errno_disarm(p->ipc_socket);
close_prot_errno_disarm(p->terminal_pts_fd);
if (p->init_ctx) {
lxc_proc_put_context_info(p->init_ctx);
p->init_ctx = NULL;
}
}
static int attach_child_main(struct attach_clone_payload *payload)
{
int lsm_fd, ret;
uid_t new_uid;
gid_t new_gid;
uid_t ns_root_uid = 0;
gid_t ns_root_gid = 0;
lxc_attach_options_t* options = payload->options;
struct lxc_proc_context_info* init_ctx = payload->init_ctx;
bool needs_lsm = (options->namespaces & CLONE_NEWNS) &&
(options->attach_flags & LXC_ATTACH_LSM) &&
init_ctx->lsm_label;
/* A description of the purpose of this functionality is provided in the
* lxc-attach(1) manual page. We have to remount here and not in the
* parent process, otherwise /proc may not properly reflect the new pid
* namespace.
*/
if (!(options->namespaces & CLONE_NEWNS) &&
(options->attach_flags & LXC_ATTACH_REMOUNT_PROC_SYS)) {
ret = lxc_attach_remount_sys_proc();
if (ret < 0)
goto on_error;
TRACE("Remounted \"/proc\" and \"/sys\"");
}
/* Now perform additional attachments. */
#if HAVE_SYS_PERSONALITY_H
if (options->attach_flags & LXC_ATTACH_SET_PERSONALITY) {
long new_personality;
if (options->personality < 0)
new_personality = init_ctx->personality;
else
new_personality = options->personality;
ret = personality(new_personality);
if (ret < 0)
goto on_error;
TRACE("Set new personality");
}
#endif
if (options->attach_flags & LXC_ATTACH_DROP_CAPABILITIES) {
ret = lxc_attach_drop_privs(init_ctx);
if (ret < 0)
goto on_error;
TRACE("Dropped capabilities");
}
/* Always set the environment (specify (LXC_ATTACH_KEEP_ENV, NULL, NULL)
* if you want this to be a no-op).
*/
ret = lxc_attach_set_environment(init_ctx,
options->env_policy,
options->extra_env_vars,
options->extra_keep_env);
if (ret < 0)
goto on_error;
TRACE("Set up environment");
/* This remark only affects fully unprivileged containers:
* Receive fd for LSM security module before we set{g,u}id(). The reason
* is that on set{g,u}id() the kernel will a) make us undumpable and b)
* we will change our effective uid. This means our effective uid will
* be different from the effective uid of the process that created us
* which means that this processs no longer has capabilities in our
* namespace including CAP_SYS_PTRACE. This means we will not be able to
* read and /proc/<pid> files for the process anymore when /proc is
* mounted with hidepid={1,2}. So let's get the lsm label fd before the
* set{g,u}id().
*/
if (needs_lsm) {
ret = lxc_abstract_unix_recv_fds(payload->ipc_socket, &lsm_fd, 1, NULL, 0);
if (ret <= 0) {
if (ret < 0)
SYSERROR("Failed to receive lsm label fd");
goto on_error;
}
TRACE("Received LSM label file descriptor %d from parent", lsm_fd);
}
if (options->stdin_fd > 0 && isatty(options->stdin_fd)) {
ret = lxc_make_controlling_terminal(options->stdin_fd);
if (ret < 0)
goto on_error;
}
if (!lxc_setgroups(0, NULL) && errno != EPERM)
goto on_error;
if (options->namespaces & CLONE_NEWUSER) {
/* Check whether nsuid 0 has a mapping. */
ns_root_uid = get_ns_uid(0);
/* Check whether nsgid 0 has a mapping. */
ns_root_gid = get_ns_gid(0);
/* If there's no mapping for nsuid 0 try to retrieve the nsuid
* init was started with.
*/
if (ns_root_uid == LXC_INVALID_UID)
lxc_attach_get_init_uidgid(&ns_root_uid, &ns_root_gid);
if (ns_root_uid == LXC_INVALID_UID)
goto on_error;
if (!lxc_switch_uid_gid(ns_root_uid, ns_root_gid))
goto on_error;
}
/* Set {u,g}id. */
if (options->uid != LXC_INVALID_UID)
new_uid = options->uid;
else
new_uid = ns_root_uid;
if (options->gid != LXC_INVALID_GID)
new_gid = options->gid;
else
new_gid = ns_root_gid;
if (needs_lsm) {
bool on_exec;
/* Change into our new LSM profile. */
on_exec = options->attach_flags & LXC_ATTACH_LSM_EXEC ? true : false;
ret = lsm_process_label_set_at(lsm_fd, init_ctx->lsm_label, on_exec);
close(lsm_fd);
if (ret < 0)
goto on_error;
TRACE("Set %s LSM label to \"%s\"", lsm_name(), init_ctx->lsm_label);
}
if ((init_ctx->container && init_ctx->container->lxc_conf &&
init_ctx->container->lxc_conf->no_new_privs) ||
(options->attach_flags & LXC_ATTACH_NO_NEW_PRIVS)) {
ret = prctl(PR_SET_NO_NEW_PRIVS, prctl_arg(1), prctl_arg(0),
prctl_arg(0), prctl_arg(0));
if (ret < 0)
goto on_error;
TRACE("Set PR_SET_NO_NEW_PRIVS");
}
if (init_ctx->container && init_ctx->container->lxc_conf &&
init_ctx->container->lxc_conf->seccomp.seccomp) {
struct lxc_conf *conf = init_ctx->container->lxc_conf;
ret = lxc_seccomp_load(conf);
if (ret < 0)
goto on_error;
TRACE("Loaded seccomp profile");
ret = lxc_seccomp_send_notifier_fd(&conf->seccomp, payload->ipc_socket);
if (ret < 0)
goto on_error;
}
close(payload->ipc_socket);
payload->ipc_socket = -EBADF;
lxc_proc_put_context_info(init_ctx);
payload->init_ctx = NULL;
/* The following is done after the communication socket is shut down.
* That way, all errors that might (though unlikely) occur up until this
* point will have their messages printed to the original stderr (if
* logging is so configured) and not the fd the user supplied, if any.
*/
/* Fd handling for stdin, stdout and stderr; ignore errors here, user
* may want to make sure the fds are closed, for example.
*/
if (options->stdin_fd >= 0 && options->stdin_fd != STDIN_FILENO)
(void)dup2(options->stdin_fd, STDIN_FILENO);
if (options->stdout_fd >= 0 && options->stdout_fd != STDOUT_FILENO)
(void)dup2(options->stdout_fd, STDOUT_FILENO);
if (options->stderr_fd >= 0 && options->stderr_fd != STDERR_FILENO)
(void)dup2(options->stderr_fd, STDERR_FILENO);
/* close the old fds */
if (options->stdin_fd > STDERR_FILENO)
close(options->stdin_fd);
if (options->stdout_fd > STDERR_FILENO)
close(options->stdout_fd);
if (options->stderr_fd > STDERR_FILENO)
close(options->stderr_fd);
/*
* Try to remove FD_CLOEXEC flag from stdin/stdout/stderr, but also
* here, ignore errors.
*/
for (int fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++) {
ret = fd_cloexec(fd, false);
if (ret < 0) {
SYSERROR("Failed to clear FD_CLOEXEC from file descriptor %d", fd);
goto on_error;
}
}
if (options->attach_flags & LXC_ATTACH_TERMINAL) {
ret = lxc_terminal_prepare_login(payload->terminal_pts_fd);
if (ret < 0) {
SYSERROR("Failed to prepare terminal file descriptor %d", payload->terminal_pts_fd);
goto on_error;
}
TRACE("Prepared terminal file descriptor %d", payload->terminal_pts_fd);
}
/* Avoid unnecessary syscalls. */
if (new_uid == ns_root_uid)
new_uid = LXC_INVALID_UID;
if (new_gid == ns_root_gid)
new_gid = LXC_INVALID_GID;
/* Make sure that the processes STDIO is correctly owned by the user that we are switching to */
ret = fix_stdio_permissions(new_uid);
if (ret)
WARN("Failed to ajust stdio permissions");
if (!lxc_switch_uid_gid(new_uid, new_gid))
goto on_error;
/* We're done, so we can now do whatever the user intended us to do. */
_exit(payload->exec_function(payload->exec_payload));
on_error:
lxc_put_attach_clone_payload(payload);
_exit(EXIT_FAILURE);
}
static int lxc_attach_terminal(struct lxc_conf *conf,
struct lxc_terminal *terminal)
{
int ret;
lxc_terminal_init(terminal);
ret = lxc_terminal_create(terminal);
if (ret < 0)
return log_error(-1, "Failed to create terminal");
/* Shift ttys to container. */
ret = lxc_terminal_map_ids(conf, terminal);
if (ret < 0) {
ERROR("Failed to chown terminal");
goto on_error;
}
return 0;
on_error:
lxc_terminal_delete(terminal);
lxc_terminal_conf_free(terminal);
return -1;
}
static int lxc_attach_terminal_mainloop_init(struct lxc_terminal *terminal,
struct lxc_epoll_descr *descr)
{
int ret;
ret = lxc_mainloop_open(descr);
if (ret < 0)
return log_error(-1, "Failed to create mainloop");
ret = lxc_terminal_mainloop_add(descr, terminal);
if (ret < 0) {
lxc_mainloop_close(descr);
return log_error(-1, "Failed to add handlers to mainloop");
}
return 0;
}
static inline void lxc_attach_terminal_close_ptmx(struct lxc_terminal *terminal)
{
close_prot_errno_disarm(terminal->ptmx);
}
static inline void lxc_attach_terminal_close_pts(struct lxc_terminal *terminal)
{
close_prot_errno_disarm(terminal->pts);
}
static inline void lxc_attach_terminal_close_peer(struct lxc_terminal *terminal)
{
close_prot_errno_disarm(terminal->peer);
}
static inline void lxc_attach_terminal_close_log(struct lxc_terminal *terminal)
{
close_prot_errno_disarm(terminal->log_fd);
}
int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function,
void *exec_payload, lxc_attach_options_t *options,
pid_t *attached_process)
{
int i, ret, status;
int ipc_sockets[2];
char *cwd, *new_cwd;
signed long personality;
pid_t attached_pid, init_pid, pid;
struct lxc_proc_context_info *init_ctx;
struct lxc_terminal terminal;
struct lxc_conf *conf;
char *name, *lxcpath;
struct attach_clone_payload payload = {0};
ret = access("/proc/self/ns", X_OK);
if (ret)
return log_error_errno(-1, errno, "Does this kernel version support namespaces?");
if (!container)
return ret_set_errno(-1, EINVAL);
if (!lxc_container_get(container))
return ret_set_errno(-1, EINVAL);
name = container->name;
lxcpath = container->config_path;
if (!options)
options = &attach_static_default_options;
init_pid = lxc_cmd_get_init_pid(name, lxcpath);
if (init_pid < 0) {
lxc_container_put(container);
return log_error(-1, "Failed to get init pid");
}
init_ctx = lxc_proc_get_context_info(init_pid);
if (!init_ctx) {
ERROR("Failed to get context of init process: %ld", (long)init_pid);
lxc_container_put(container);
return -1;
}
init_ctx->container = container;