This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
frontend.c
1898 lines (1666 loc) · 51.3 KB
/
frontend.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
/* frontend.c: definitions of the dbus and scm-enhanced dbus
* frontend routines.
*
* Copyright © 2013 Stephane Graber
* Author: Stephane Graber <[email protected]>
* Copyright © 2014 Canonical Group Limited
* Author: Serge Hallyn <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define CGM_FRONTEND_C
#include <frontend.h>
int daemonise = FALSE;
int sigstop = FALSE;
bool setns_pid_supported = false;
unsigned long mypidns;
bool setns_user_supported = false;
unsigned long myuserns;
bool sane_cgroup(const char *cgroup)
{
if (!cgroup)
return false;
if (strstr(cgroup, ".."))
return false;
return true;
}
/* This function is done at the start of every Scm-enhanced transaction */
static struct scm_sock_data *alloc_scm_sock_data(NihDBusMessage *message,
int fd, enum req_type t)
{
struct scm_sock_data *d;
int optval = 1, dbusfd;
socklen_t len;
if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)) == -1) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Failed to set passcred: %s", strerror(errno));
return NULL;
}
d = NIH_MUST( nih_alloc(NULL, sizeof(*d)) );
memset(d, 0, sizeof(*d));
d->fd = fd;
d->type = t;
if (!dbus_connection_get_socket(message->connection, &dbusfd)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get client socket.");
return NULL;
}
/* Read the proxy's credentials from dbus fd */
len = sizeof(struct ucred);
if (getsockopt(dbusfd, SOL_SOCKET, SO_PEERCRED, &d->pcred, &len) < 0) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get peer cred: %s",
strerror(errno));
return NULL;
}
return d;
}
/*
* All Scm-enhanced transactions take at least one SCM cred,
* the requestor's. Some require a second SCM cred to identify
* a pid or uid/gid:
*/
static bool need_two_creds(enum req_type t)
{
switch (t) {
case REQ_TYPE_GET_PID:
case REQ_TYPE_GET_PID_ABS:
case REQ_TYPE_MOVE_PID:
case REQ_TYPE_MOVE_PID_ABS:
case REQ_TYPE_CHOWN:
return true;
default:
return false;
}
}
static void scm_sock_error_handler (void *data, NihIo *io)
{
struct scm_sock_data *d = data;
NihError *error = nih_error_get ();
nih_free(error);
d->fd = -1;
}
static void scm_sock_close (struct scm_sock_data *data, NihIo *io)
{
nih_assert (data);
nih_assert (io);
nih_free (io);
// Only delete data struct after io, because freeing io may call methods
// like error_handler that use this data struct.
if (data->fd != -1)
close (data->fd);
nih_free (data);
}
/*
* Write a char over the socket to tell the client we're ready for
* the next SCM credential.
*/
static bool kick_fd_client(int fd)
{
char buf = '1';
if (write(fd, &buf, 1) != 1) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Failed to start write on scm fd: %s", strerror(errno));
return false;
}
return true;
}
/*
* Called when an scm credential has been received. If this was
* the first of two expected creds, then kick the client again
* and wait (async) for the next credential. Otherwise, call
* the appropriate completion function to finish the transaction.
*/
static void sock_scm_reader(struct scm_sock_data *data,
NihIo *io, const char *buf, size_t len)
{
struct ucred ucred;
if (!get_nih_io_creds(data, io, &ucred)) {
nih_error("failed to read ucred");
nih_io_shutdown(io);
return;
}
if (data->step == 0) {
memcpy(&data->rcred, &ucred, sizeof(struct ucred));
if (need_two_creds(data->type)) {
data->step = 1;
if (!kick_fd_client(data->fd))
nih_io_shutdown(io);
return;
}
} else
memcpy(&data->vcred, &ucred, sizeof(struct ucred));
switch (data->type) {
case REQ_TYPE_GET_PID: get_pid_scm_complete(data); break;
case REQ_TYPE_GET_PID_ABS: get_pid_abs_scm_complete(data); break;
case REQ_TYPE_MOVE_PID: move_pid_scm_complete(data); break;
case REQ_TYPE_MOVE_PID_ABS: move_pid_abs_scm_complete(data); break;
case REQ_TYPE_CREATE: create_scm_complete(data); break;
case REQ_TYPE_CHOWN: chown_scm_complete(data); break;
case REQ_TYPE_CHMOD: chmod_scm_complete(data); break;
case REQ_TYPE_GET_VALUE: get_value_complete(data); break;
case REQ_TYPE_SET_VALUE: set_value_complete(data); break;
case REQ_TYPE_REMOVE: remove_scm_complete(data); break;
case REQ_TYPE_GET_TASKS: get_tasks_scm_complete(data); break;
case REQ_TYPE_LIST_CHILDREN: list_children_scm_complete(data); break;
case REQ_TYPE_REMOVE_ON_EMPTY: remove_on_empty_scm_complete(data); break;
case REQ_TYPE_PRUNE: prune_scm_complete(data); break;
case REQ_TYPE_GET_TASKS_RECURSIVE: get_tasks_recursive_scm_complete(data); break;
case REQ_TYPE_LISTKEYS: list_keys_scm_complete(data); break;
default:
nih_fatal("%s: bad req_type %d", __func__, data->type);
exit(1);
}
nih_io_shutdown(io);
}
int cgmanager_ping (void *data, NihDBusMessage *message, int junk)
{
if (message == NULL) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"message was null");
return -1;
}
return 0;
}
void get_pid_scm_complete(struct scm_sock_data *data)
{
// output will be nih_alloced with data as parent, and therefore
// freed when data is freed.
char *output = NULL;
int ret;
ret = get_pid_cgroup_main(data, data->controller, data->pcred,
data->rcred, data->vcred, &output);
if (ret == 0)
ret = write(data->fd, output, strlen(output)+1);
else
// Let the client know it failed
ret = write(data->fd, &data->rcred, 0);
if (ret < 0)
nih_error("GetPidCgroupScm: Error writing final result to client: %s",
strerror(errno));
}
/*
* This is one of the dbus callbacks.
* Caller requests the cgroup of @pid in a given @controller
*/
int cgmanager_get_pid_cgroup_scm (void *data, NihDBusMessage *message,
char *controller, int sockfd)
{
struct scm_sock_data *d;
d = alloc_scm_sock_data(message, sockfd, REQ_TYPE_GET_PID);
if (!d)
return -1;
d->controller = NIH_MUST( nih_strdup(d, controller) );
if (!nih_io_reopen(NULL, sockfd, NIH_IO_MESSAGE,
(NihIoReader) sock_scm_reader,
(NihIoCloseHandler) scm_sock_close,
scm_sock_error_handler, d)) {
NihError *error = nih_error_steal ();
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Failed queue scm message: %s", error->message);
nih_free(error);
return -1;
}
if (!kick_fd_client(sockfd))
return -1;
return 0;
}
/* GetPidCgroup */
/*
* This is one of the dbus callbacks.
* Caller requests the cgroup of @pid in a given @controller
*/
int cgmanager_get_pid_cgroup (void *data, NihDBusMessage *message,
char *controller, int plain_pid, char **output)
{
int fd = 0, ret;
struct ucred rcred, vcred;
socklen_t len;
if (message == NULL) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"message was null");
return -1;
}
if (!dbus_connection_get_socket(message->connection, &fd)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get client socket.");
return -1;
}
len = sizeof(struct ucred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &rcred, &len) < 0) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get peer cred: %s",
strerror(errno));
return -1;
}
nih_info (_("GetPidCgroup: Client fd is: %d (pid=%d, uid=%u, gid=%u)"),
fd, rcred.pid, rcred.uid, rcred.gid);
/*
* getpidcgroup results cannot make sense as the pid is not
* translated. Note that on an old enough kernel we cannot detect
* this situation. In that case we allow it - it will confuse the
* caller, but cause no harm
*/
if (!is_same_pidns(rcred.pid)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"GetPidCgroup called from non-init namespace");
return -1;
}
vcred.uid = 0;
vcred.gid = 0;
vcred.pid = plain_pid;
ret = get_pid_cgroup_main(message, controller, rcred, rcred, vcred, output);
if (ret) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"invalid request");
return -1;
}
return 0;
}
void get_pid_abs_scm_complete(struct scm_sock_data *data)
{
// output will be nih_alloced with data as parent, and therefore
// freed when data is freed.
char *output = NULL;
int ret;
ret = get_pid_cgroup_abs_main(data, data->controller, data->pcred,
data->rcred, data->vcred, &output);
if (ret == 0)
ret = write(data->fd, output, strlen(output)+1);
else
// Let the client know it failed
ret = write(data->fd, &data->rcred, 0);
if (ret < 0)
nih_error("GetPidCgroupAbsScm: Error writing final result to client: %s",
strerror(errno));
}
/*
* This is one of the dbus callbacks.
* Caller requests the cgroup of @pid in a given @controller, relative
* to the proxy's
*/
int cgmanager_get_pid_cgroup_abs_scm (void *data, NihDBusMessage *message,
char *controller, int sockfd)
{
struct scm_sock_data *d;
d = alloc_scm_sock_data(message, sockfd, REQ_TYPE_GET_PID_ABS);
if (!d)
return -1;
d->controller = NIH_MUST( nih_strdup(d, controller) );
if (!nih_io_reopen(NULL, sockfd, NIH_IO_MESSAGE,
(NihIoReader) sock_scm_reader,
(NihIoCloseHandler) scm_sock_close,
scm_sock_error_handler, d)) {
NihError *error = nih_error_steal ();
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Failed queue scm message: %s", error->message);
nih_free(error);
return -1;
}
if (!kick_fd_client(sockfd))
return -1;
return 0;
}
/* GetPidCgroup */
/*
* This is one of the dbus callbacks.
* Caller requests the cgroup of @pid in a given @controller relative
* to the proxy's
*/
int cgmanager_get_pid_cgroup_abs (void *data, NihDBusMessage *message,
char *controller, int plain_pid, char **output)
{
int fd = 0, ret;
struct ucred rcred, vcred;
socklen_t len;
if (message == NULL) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"message was null");
return -1;
}
if (!dbus_connection_get_socket(message->connection, &fd)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get client socket.");
return -1;
}
len = sizeof(struct ucred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &rcred, &len) < 0) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get peer cred: %s",
strerror(errno));
return -1;
}
nih_info (_("GetPidCgroupAbs: Client fd is: %d (pid=%d, uid=%u, gid=%u)"),
fd, rcred.pid, rcred.uid, rcred.gid);
/*
* getpidcgroup results cannot make sense as the pid is not
* translated. Note that on an old enough kernel we cannot detect
* this situation. In that case we allow it - it will confuse the
* caller, but cause no harm
*/
if (!is_same_pidns(rcred.pid)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"GetPidCgroupAbs called from non-init namespace");
return -1;
}
vcred.uid = 0;
vcred.gid = 0;
vcred.pid = plain_pid;
#ifdef CGMANAGER
/*
* A plain dbus request to escape cgroup root was made by a root
* owned task in cgmanager's namespace. We will send ourselves as the
* proxy.
*/
struct ucred mycred = {
.pid = getpid(),
.uid = getuid(),
.gid = getgid()
};
#else
/*
* This is the !CGMANAGER case. We are in the proxy. We don't
* support chained proxying anyway, so it is simple - the requestor
* is the proxy at this point; then we will proxy the call on to
* the cgmanager
*/
#define mycred rcred
#endif
ret = get_pid_cgroup_abs_main(message, controller, mycred, rcred, vcred, output);
if (ret) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"invalid request");
return -1;
}
return 0;
}
void move_pid_scm_complete(struct scm_sock_data *data)
{
char b = '0';
if (move_pid_main(data->controller, data->cgroup, data->pcred,
data->rcred, data->vcred) == 0)
b = '1';
if (write(data->fd, &b, 1) < 0)
nih_error("MovePidScm: Error writing final result to client");
}
int cgmanager_move_pid_scm (void *data, NihDBusMessage *message,
const char *controller, const char *cgroup,
int sockfd)
{
struct scm_sock_data *d;
d = alloc_scm_sock_data(message, sockfd, REQ_TYPE_MOVE_PID);
if (!d)
return -1;
d->controller = NIH_MUST( nih_strdup(d, controller) );
d->cgroup = NIH_MUST( nih_strdup(d, cgroup) );
if (!nih_io_reopen(NULL, sockfd, NIH_IO_MESSAGE,
(NihIoReader) sock_scm_reader,
(NihIoCloseHandler) scm_sock_close,
scm_sock_error_handler, d)) {
NihError *error = nih_error_steal ();
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Failed queue scm message: %s", error->message);
nih_free(error);
return -1;
}
if (!kick_fd_client(sockfd))
return -1;
return 0;
}
/*
* This is one of the dbus callbacks.
* Caller requests moving a @pid to a particular cgroup identified
* by the name (@cgroup) and controller type (@controller).
*/
int cgmanager_move_pid (void *data, NihDBusMessage *message,
const char *controller, const char *cgroup, int plain_pid)
{
int fd = 0, ret;
struct ucred rcred, vcred;
socklen_t len;
if (message == NULL) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"message was null");
return -1;
}
if (!dbus_connection_get_socket(message->connection, &fd)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get client socket.");
return -1;
}
len = sizeof(struct ucred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &rcred, &len) < 0) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get peer cred: %s",
strerror(errno));
return -1;
}
nih_info (_("MovePid: Client fd is: %d (pid=%d, uid=%u, gid=%u)"),
fd, rcred.pid, rcred.uid, rcred.gid);
/* If task is in a different namespace, require a proxy */
if (!is_same_pidns(rcred.pid)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Escape request from different namespace requires a proxy");
return -1;
}
vcred.uid = 0;
vcred.gid = 0;
vcred.pid = plain_pid;
ret = move_pid_main(controller, cgroup, rcred, rcred, vcred);
if (ret)
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"invalid request");
return ret;
}
void move_pid_abs_scm_complete(struct scm_sock_data *data)
{
char b = '0';
if (move_pid_abs_main(data->controller, data->cgroup, data->pcred,
data->rcred, data->vcred) == 0)
b = '1';
if (write(data->fd, &b, 1) < 0)
nih_error("MovePidScm: Error writing final result to client");
}
int cgmanager_move_pid_abs_scm (void *data, NihDBusMessage *message,
const char *controller, const char *cgroup,
int sockfd)
{
struct scm_sock_data *d;
d = alloc_scm_sock_data(message, sockfd, REQ_TYPE_MOVE_PID_ABS);
if (!d)
return -1;
d->controller = NIH_MUST( nih_strdup(d, controller) );
d->cgroup = NIH_MUST( nih_strdup(d, cgroup) );
if (!nih_io_reopen(NULL, sockfd, NIH_IO_MESSAGE,
(NihIoReader) sock_scm_reader,
(NihIoCloseHandler) scm_sock_close,
scm_sock_error_handler, d)) {
NihError *error = nih_error_steal ();
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Failed queue scm message: %s", error->message);
nih_free(error);
return -1;
}
if (!kick_fd_client(sockfd))
return -1;
return 0;
}
int cgmanager_move_pid_abs (void *data, NihDBusMessage *message,
const char *controller, const char *cgroup, int plain_pid)
{
int fd = 0, ret;
struct ucred rcred, vcred;
socklen_t len;
if (message == NULL) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"message was null");
return -1;
}
if (!dbus_connection_get_socket(message->connection, &fd)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get client socket.");
return -1;
}
len = sizeof(struct ucred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &rcred, &len) < 0) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get peer cred: %s",
strerror(errno));
return -1;
}
nih_info (_("MovePid: Client fd is: %d (pid=%d, uid=%u, gid=%u)"),
fd, rcred.pid, rcred.uid, rcred.gid);
vcred.uid = 0;
vcred.gid = 0;
vcred.pid = plain_pid;
#ifdef CGMANAGER
/*
* On an older kernel, require a proxy
*/
if (!setns_pid_supported) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"A proxy is required");
return -1;
}
#endif
/* If task is in a different namespace, require a proxy */
if (!is_same_pidns(rcred.pid)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Escape request from different namespace requires a proxy");
return -1;
}
#ifdef CGMANAGER
/*
* A plain dbus request to escape cgroup root was made by a root
* owned task in cgmanager's namespace. We will send ourselves as the
* proxy.
*/
struct ucred mycred = {
.pid = getpid(),
.uid = getuid(),
.gid = getgid()
};
#else
/*
* This is the !CGMANAGER case. We are in the proxy. We don't
* support chained proxying anyway, so it is simple - the requestor
* is the proxy at this point; then we will proxy the call on to
* the cgmanager
*/
#define mycred rcred
#endif
ret = move_pid_abs_main(controller, cgroup, mycred, rcred, vcred);
if (ret)
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"invalid request");
return ret;
}
void create_scm_complete(struct scm_sock_data *data)
{
char b = '0';
int32_t existed;
if (create_main(data->controller, data->cgroup, data->pcred,
data->rcred, &existed) == 0)
b = existed == 1 ? '2' : '1';
if (write(data->fd, &b, 1) < 0)
nih_error("createScm: Error writing final result to client");
}
int cgmanager_create_scm (void *data, NihDBusMessage *message,
const char *controller, const char *cgroup, int sockfd)
{
struct scm_sock_data *d;
d = alloc_scm_sock_data(message, sockfd, REQ_TYPE_CREATE);
if (!d)
return -1;
d->controller = NIH_MUST( nih_strdup(d, controller) );
d->cgroup = NIH_MUST( nih_strdup(d, cgroup) );
if (!nih_io_reopen(NULL, sockfd, NIH_IO_MESSAGE,
(NihIoReader) sock_scm_reader,
(NihIoCloseHandler) scm_sock_close,
scm_sock_error_handler, d)) {
NihError *error = nih_error_steal ();
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Failed queue scm message: %s", error->message);
nih_free(error);
return -1;
}
if (!kick_fd_client(sockfd))
return -1;
return 0;
}
/*
* This is one of the dbus callbacks.
* Caller requests creating a new @cgroup name of type @controller.
* @name is taken to be relative to the caller's cgroup and may not
* start with / or .. .
*/
int cgmanager_create (void *data, NihDBusMessage *message,
const char *controller, const char *cgroup, int32_t *existed)
{
int fd = 0, ret;
struct ucred rcred;
socklen_t len;
*existed = -1;
if (message == NULL) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"message was null");
return -1;
}
if (!dbus_connection_get_socket(message->connection, &fd)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get client socket.");
return -1;
}
len = sizeof(struct ucred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &rcred, &len) < 0) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get peer cred: %s",
strerror(errno));
return -1;
}
nih_info (_("Create: Client fd is: %d (pid=%d, uid=%u, gid=%u)"),
fd, rcred.pid, rcred.uid, rcred.gid);
ret = create_main(controller, cgroup, rcred, rcred, existed);
if (ret)
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"invalid request");
nih_info(_("%s: returning %d; existed is %d"), __func__, ret, *existed);
return ret;
}
void chown_scm_complete(struct scm_sock_data *data)
{
char b = '0';
if (chown_main(data->controller, data->cgroup, data->pcred,
data->rcred, data->vcred) == 0)
b = '1';
if (write(data->fd, &b, 1) < 0)
nih_error("ChownScm: Error writing final result to client");
}
int cgmanager_chown_scm (void *data, NihDBusMessage *message,
const char *controller, const char *cgroup, int sockfd)
{
struct scm_sock_data *d;
d = alloc_scm_sock_data(message, sockfd, REQ_TYPE_CHOWN);
if (!d)
return -1;
d->controller = NIH_MUST( nih_strdup(d, controller) );
d->cgroup = NIH_MUST( nih_strdup(d, cgroup) );
if (!nih_io_reopen(NULL, sockfd, NIH_IO_MESSAGE,
(NihIoReader) sock_scm_reader,
(NihIoCloseHandler) scm_sock_close,
scm_sock_error_handler, d)) {
NihError *error = nih_error_steal ();
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Failed queue scm message: %s", error->message);
nih_free(error);
return -1;
}
if (!kick_fd_client(sockfd))
return -1;
return 0;
}
/*
* This is one of the dbus callbacks.
* Caller requests chowning a cgroup @name in controller @cgroup to a
* particular @uid. The uid must be passed in as an scm_cred so the
* kernel translates it for us. @r must be root in its own user ns.
*
* If we are asked to chown /b to UID, then we will chown:
* /b itself, /b/tasks, and /b/procs. Any other files in /b will not be
* chown. UID can then create subdirs of /b, but not raise his limits.
*/
int cgmanager_chown (void *data, NihDBusMessage *message,
const char *controller, const char *cgroup, int uid, int gid)
{
int fd = 0, ret;
struct ucred rcred, vcred;
socklen_t len;
if (message == NULL) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"message was null");
return -1;
}
if (!dbus_connection_get_socket(message->connection, &fd)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get client socket.");
return -1;
}
len = sizeof(struct ucred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &rcred, &len) < 0) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get peer cred: %s",
strerror(errno));
return -1;
}
nih_info (_("Chown: Client fd is: %d (pid=%d, uid=%u, gid=%u)"),
fd, rcred.pid, rcred.uid, rcred.gid);
/*
* If chown is called from a different user namespace, then the
* results cannot make sense. Note that on an old enough kernel
* we cannot detect this. However, in that case the caller will
* not have privilege so will simply get a confusing -EPERM. In
* other words, we are doing this as a courtesy when possible
*/
if (!is_same_userns(rcred.pid)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"chown called from different user namespace");
return -1;
}
vcred.pid = getpid(); // cgmanager ignores this
vcred.uid = uid;
vcred.gid = gid;
ret = chown_main(controller, cgroup, rcred, rcred, vcred);
if (ret)
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"invalid request");
return ret;
}
void chmod_scm_complete(struct scm_sock_data *data)
{
char b = '0';
if (chmod_main(data->controller, data->cgroup, data->file,
data->pcred, data->rcred, data->mode) == 0)
b = '1';
if (write(data->fd, &b, 1) < 0)
nih_error("ChownScm: Error writing final result to client");
}
int cgmanager_chmod_scm (void *data, NihDBusMessage *message,
const char *controller, const char *cgroup,
const char *file, int mode, int sockfd)
{
struct scm_sock_data *d;
d = alloc_scm_sock_data(message, sockfd, REQ_TYPE_CHMOD);
if (!d)
return -1;
d->controller = NIH_MUST( nih_strdup(d, controller) );
d->cgroup = NIH_MUST( nih_strdup(d, cgroup) );
d->mode = mode;
d->file = NIH_MUST( nih_strdup(d, file) );
if (!nih_io_reopen(NULL, sockfd, NIH_IO_MESSAGE,
(NihIoReader) sock_scm_reader,
(NihIoCloseHandler) scm_sock_close,
scm_sock_error_handler, d)) {
NihError *error = nih_error_steal ();
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Failed queue scm message: %s", error->message);
nih_free(error);
return -1;
}
if (!kick_fd_client(sockfd))
return -1;
return 0;
}
/*
* This is one of the dbus callbacks. Caller requests chmoding a file @path in
* cgroup @name in controller @cgroup to a new @mode.
*/
int cgmanager_chmod (void *data, NihDBusMessage *message,
const char *controller, const char *cgroup,
const char *file, int mode)
{
int fd = 0, ret;
struct ucred rcred;
socklen_t len;
if (message == NULL) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"message was null");
return -1;
}
if (!dbus_connection_get_socket(message->connection, &fd)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get client socket.");
return -1;
}
len = sizeof(struct ucred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &rcred, &len) < 0) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get peer cred: %s",
strerror(errno));
return -1;
}
nih_info (_("Chown: Client fd is: %d (pid=%d, uid=%u, gid=%u)"),
fd, rcred.pid, rcred.uid, rcred.gid);
ret = chmod_main(controller, cgroup, file, rcred, rcred, mode);
if (ret)
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"invalid request");
return ret;
}
void get_value_complete(struct scm_sock_data *data)
{
char *output = NULL;
int ret;
if (!get_value_main(data, data->controller, data->cgroup, data->key,
data->pcred, data->rcred, &output))
ret = write(data->fd, output, strlen(output)+1);
else
ret = write(data->fd, &data->rcred, 0); // kick the client
if (ret < 0)
nih_error("GetValueScm: Error writing final result to client");
}
int cgmanager_get_value_scm (void *data, NihDBusMessage *message,
char *controller, const char *req_cgroup,
const char *key, int sockfd)
{
struct scm_sock_data *d;
d = alloc_scm_sock_data(message, sockfd, REQ_TYPE_GET_VALUE);
if (!d)
return -1;
d->controller = NIH_MUST( nih_strdup(d, controller) );
d->cgroup = NIH_MUST( nih_strdup(d, req_cgroup) );
d->key = NIH_MUST( nih_strdup(d, key) );
if (!nih_io_reopen(NULL, sockfd, NIH_IO_MESSAGE,
(NihIoReader) sock_scm_reader,
(NihIoCloseHandler) scm_sock_close,
scm_sock_error_handler, d)) {
NihError *error = nih_error_steal ();
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Failed queue scm message: %s", error->message);
nih_free(error);
return -1;
}
if (!kick_fd_client(sockfd))
return -1;
return 0;
}
/*
* This is one of the dbus callbacks.
* Caller requests the value of a particular cgroup file.
* @controller is the controller, @req_cgroup the cgroup name, and @key the
* file being queried (i.e. memory.usage_in_bytes). @req_cgroup is relative
* to the caller's cgroup, unless it begins with '/' or '..'.
*
* XXX Should '/' be disallowed, only '..' allowed? Otherwise callers can't
* pretend to be the cgroup root which is annoying in itself
*/
int cgmanager_get_value (void *data, NihDBusMessage *message,
char *controller, const char *req_cgroup,
const char *key, char **value)
{
int fd = 0, ret;
struct ucred rcred;
socklen_t len;
if (message == NULL) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Message was NULL");
return -1;
}
if (!dbus_connection_get_socket(message->connection, &fd)) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get client socket.");
return -1;
}
len = sizeof(struct ucred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &rcred, &len) < 0) {
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"Could not get peer cred: %s",
strerror(errno));
return -1;
}
nih_info (_("GetValue: Client fd is: %d (pid=%d, uid=%u, gid=%u)"),
fd, rcred.pid, rcred.uid, rcred.gid);
ret = get_value_main(message, controller, req_cgroup, key, rcred, rcred, value);
if (ret)
nih_dbus_error_raise_printf (DBUS_ERROR_INVALID_ARGS,
"invalid request");
return ret;
}
void set_value_complete(struct scm_sock_data *data)
{