-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathkern_prot.c
3066 lines (2746 loc) · 73.3 KB
/
kern_prot.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: BSD-3-Clause
*
* Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
* The Regents of the University of California.
* (c) UNIX System Laboratories, Inc.
* Copyright (c) 2000-2001 Robert N. M. Watson.
* All rights reserved.
*
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* System calls related to processes and protection
*/
#include <sys/cdefs.h>
#include "opt_inet.h"
#include "opt_inet6.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/abi_compat.h>
#include <sys/acct.h>
#include <sys/kdb.h>
#include <sys/kernel.h>
#include <sys/libkern.h>
#include <sys/lock.h>
#include <sys/loginclass.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/ptrace.h>
#include <sys/refcount.h>
#include <sys/sx.h>
#include <sys/priv.h>
#include <sys/proc.h>
#ifdef COMPAT_43
#include <sys/sysent.h>
#endif
#include <sys/sysproto.h>
#include <sys/jail.h>
#include <sys/racct.h>
#include <sys/rctl.h>
#include <sys/resourcevar.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/syscallsubr.h>
#include <sys/sysctl.h>
#ifdef MAC
#include <security/mac/mac_syscalls.h>
#endif
#include <vm/uma.h>
#ifdef REGRESSION
FEATURE(regression,
"Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
#endif
#include <security/audit/audit.h>
#include <security/mac/mac_framework.h>
static MALLOC_DEFINE(M_CRED, "cred", "credentials");
SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
"BSD security policy");
static void crfree_final(struct ucred *cr);
static inline void
groups_check_positive_len(int ngrp)
{
MPASS2(ngrp >= 0, "negative number of groups");
MPASS2(ngrp != 0, "at least one group expected (effective GID)");
}
static inline void
groups_check_max_len(int ngrp)
{
MPASS2(ngrp <= ngroups_max + 1, "too many groups");
}
static void groups_normalize(int *ngrp, gid_t *groups);
static void crsetgroups_internal(struct ucred *cr, int ngrp,
const gid_t *groups);
static int cr_canseeotheruids(struct ucred *u1, struct ucred *u2);
static int cr_canseeothergids(struct ucred *u1, struct ucred *u2);
static int cr_canseejailproc(struct ucred *u1, struct ucred *u2);
#ifndef _SYS_SYSPROTO_H_
struct getpid_args {
int dummy;
};
#endif
/* ARGSUSED */
int
sys_getpid(struct thread *td, struct getpid_args *uap)
{
struct proc *p = td->td_proc;
td->td_retval[0] = p->p_pid;
#if defined(COMPAT_43)
if (SV_PROC_FLAG(p, SV_AOUT))
td->td_retval[1] = kern_getppid(td);
#endif
return (0);
}
#ifndef _SYS_SYSPROTO_H_
struct getppid_args {
int dummy;
};
#endif
/* ARGSUSED */
int
sys_getppid(struct thread *td, struct getppid_args *uap)
{
td->td_retval[0] = kern_getppid(td);
return (0);
}
int
kern_getppid(struct thread *td)
{
struct proc *p = td->td_proc;
return (p->p_oppid);
}
/*
* Get process group ID; note that POSIX getpgrp takes no parameter.
*/
#ifndef _SYS_SYSPROTO_H_
struct getpgrp_args {
int dummy;
};
#endif
int
sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
{
struct proc *p = td->td_proc;
PROC_LOCK(p);
td->td_retval[0] = p->p_pgrp->pg_id;
PROC_UNLOCK(p);
return (0);
}
/* Get an arbitrary pid's process group id */
#ifndef _SYS_SYSPROTO_H_
struct getpgid_args {
pid_t pid;
};
#endif
int
sys_getpgid(struct thread *td, struct getpgid_args *uap)
{
struct proc *p;
int error;
if (uap->pid == 0) {
p = td->td_proc;
PROC_LOCK(p);
} else {
p = pfind(uap->pid);
if (p == NULL)
return (ESRCH);
error = p_cansee(td, p);
if (error) {
PROC_UNLOCK(p);
return (error);
}
}
td->td_retval[0] = p->p_pgrp->pg_id;
PROC_UNLOCK(p);
return (0);
}
/*
* Get an arbitrary pid's session id.
*/
#ifndef _SYS_SYSPROTO_H_
struct getsid_args {
pid_t pid;
};
#endif
int
sys_getsid(struct thread *td, struct getsid_args *uap)
{
return (kern_getsid(td, uap->pid));
}
int
kern_getsid(struct thread *td, pid_t pid)
{
struct proc *p;
int error;
if (pid == 0) {
p = td->td_proc;
PROC_LOCK(p);
} else {
p = pfind(pid);
if (p == NULL)
return (ESRCH);
error = p_cansee(td, p);
if (error) {
PROC_UNLOCK(p);
return (error);
}
}
td->td_retval[0] = p->p_session->s_sid;
PROC_UNLOCK(p);
return (0);
}
#ifndef _SYS_SYSPROTO_H_
struct getuid_args {
int dummy;
};
#endif
/* ARGSUSED */
int
sys_getuid(struct thread *td, struct getuid_args *uap)
{
td->td_retval[0] = td->td_ucred->cr_ruid;
#if defined(COMPAT_43)
td->td_retval[1] = td->td_ucred->cr_uid;
#endif
return (0);
}
#ifndef _SYS_SYSPROTO_H_
struct geteuid_args {
int dummy;
};
#endif
/* ARGSUSED */
int
sys_geteuid(struct thread *td, struct geteuid_args *uap)
{
td->td_retval[0] = td->td_ucred->cr_uid;
return (0);
}
#ifndef _SYS_SYSPROTO_H_
struct getgid_args {
int dummy;
};
#endif
/* ARGSUSED */
int
sys_getgid(struct thread *td, struct getgid_args *uap)
{
td->td_retval[0] = td->td_ucred->cr_rgid;
#if defined(COMPAT_43)
td->td_retval[1] = td->td_ucred->cr_groups[0];
#endif
return (0);
}
/*
* Get effective group ID. The "egid" is groups[0], and could be obtained
* via getgroups. This syscall exists because it is somewhat painful to do
* correctly in a library function.
*/
#ifndef _SYS_SYSPROTO_H_
struct getegid_args {
int dummy;
};
#endif
/* ARGSUSED */
int
sys_getegid(struct thread *td, struct getegid_args *uap)
{
td->td_retval[0] = td->td_ucred->cr_groups[0];
return (0);
}
#ifndef _SYS_SYSPROTO_H_
struct getgroups_args {
int gidsetsize;
gid_t *gidset;
};
#endif
int
sys_getgroups(struct thread *td, struct getgroups_args *uap)
{
struct ucred *cred;
int ngrp, error;
cred = td->td_ucred;
ngrp = cred->cr_ngroups;
if (uap->gidsetsize == 0) {
error = 0;
goto out;
}
if (uap->gidsetsize < ngrp)
return (EINVAL);
error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
out:
td->td_retval[0] = ngrp;
return (error);
}
#ifndef _SYS_SYSPROTO_H_
struct setsid_args {
int dummy;
};
#endif
/* ARGSUSED */
int
sys_setsid(struct thread *td, struct setsid_args *uap)
{
struct pgrp *pgrp;
int error;
struct proc *p = td->td_proc;
struct pgrp *newpgrp;
struct session *newsess;
pgrp = NULL;
newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
again:
error = 0;
sx_xlock(&proctree_lock);
if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
if (pgrp != NULL)
PGRP_UNLOCK(pgrp);
error = EPERM;
} else {
error = enterpgrp(p, p->p_pid, newpgrp, newsess);
if (error == ERESTART)
goto again;
MPASS(error == 0);
td->td_retval[0] = p->p_pid;
newpgrp = NULL;
newsess = NULL;
}
sx_xunlock(&proctree_lock);
uma_zfree(pgrp_zone, newpgrp);
free(newsess, M_SESSION);
return (error);
}
/*
* set process group (setpgid/old setpgrp)
*
* caller does setpgid(targpid, targpgid)
*
* pid must be caller or child of caller (ESRCH)
* if a child
* pid must be in same session (EPERM)
* pid can't have done an exec (EACCES)
* if pgid != pid
* there must exist some pid in same session having pgid (EPERM)
* pid must not be session leader (EPERM)
*/
#ifndef _SYS_SYSPROTO_H_
struct setpgid_args {
int pid; /* target process id */
int pgid; /* target pgrp id */
};
#endif
/* ARGSUSED */
int
sys_setpgid(struct thread *td, struct setpgid_args *uap)
{
struct proc *curp = td->td_proc;
struct proc *targp; /* target process */
struct pgrp *pgrp; /* target pgrp */
int error;
struct pgrp *newpgrp;
if (uap->pgid < 0)
return (EINVAL);
newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
again:
error = 0;
sx_xlock(&proctree_lock);
if (uap->pid != 0 && uap->pid != curp->p_pid) {
if ((targp = pfind(uap->pid)) == NULL) {
error = ESRCH;
goto done;
}
if (!inferior(targp)) {
PROC_UNLOCK(targp);
error = ESRCH;
goto done;
}
if ((error = p_cansee(td, targp))) {
PROC_UNLOCK(targp);
goto done;
}
if (targp->p_pgrp == NULL ||
targp->p_session != curp->p_session) {
PROC_UNLOCK(targp);
error = EPERM;
goto done;
}
if (targp->p_flag & P_EXEC) {
PROC_UNLOCK(targp);
error = EACCES;
goto done;
}
PROC_UNLOCK(targp);
} else
targp = curp;
if (SESS_LEADER(targp)) {
error = EPERM;
goto done;
}
if (uap->pgid == 0)
uap->pgid = targp->p_pid;
if ((pgrp = pgfind(uap->pgid)) == NULL) {
if (uap->pgid == targp->p_pid) {
error = enterpgrp(targp, uap->pgid, newpgrp,
NULL);
if (error == 0)
newpgrp = NULL;
} else
error = EPERM;
} else {
if (pgrp == targp->p_pgrp) {
PGRP_UNLOCK(pgrp);
goto done;
}
if (pgrp->pg_id != targp->p_pid &&
pgrp->pg_session != curp->p_session) {
PGRP_UNLOCK(pgrp);
error = EPERM;
goto done;
}
PGRP_UNLOCK(pgrp);
error = enterthispgrp(targp, pgrp);
}
done:
KASSERT(error == 0 || newpgrp != NULL,
("setpgid failed and newpgrp is NULL"));
if (error == ERESTART)
goto again;
sx_xunlock(&proctree_lock);
uma_zfree(pgrp_zone, newpgrp);
return (error);
}
static int
gidp_cmp(const void *p1, const void *p2)
{
const gid_t g1 = *(const gid_t *)p1;
const gid_t g2 = *(const gid_t *)p2;
return ((g1 > g2) - (g1 < g2));
}
/*
* Final storage for groups (including the effective GID) will be returned via
* 'groups'. '*groups' must be NULL on input, and if not equal to 'smallgroups'
* on output, must be freed (M_TEMP) *even if* an error is returned.
*/
static int
kern_setcred_copyin_supp_groups(struct setcred *const wcred,
const u_int flags, gid_t *const smallgroups, gid_t **const groups)
{
MPASS(*groups == NULL);
if (flags & SETCREDF_SUPP_GROUPS) {
int error;
/*
* Check for the limit for number of groups right now in order
* to limit the amount of bytes to copy.
*/
if (wcred->sc_supp_groups_nb > ngroups_max)
return (EINVAL);
/*
* Since we are going to be copying the supplementary groups
* from userland, make room also for the effective GID right
* now, to avoid having to allocate and copy again the
* supplementary groups.
*/
*groups = wcred->sc_supp_groups_nb < CRED_SMALLGROUPS_NB ?
smallgroups : malloc((wcred->sc_supp_groups_nb + 1) *
sizeof(*groups), M_TEMP, M_WAITOK);
error = copyin(wcred->sc_supp_groups, *groups + 1,
wcred->sc_supp_groups_nb * sizeof(*groups));
if (error != 0)
return (error);
wcred->sc_supp_groups = *groups + 1;
} else {
wcred->sc_supp_groups_nb = 0;
wcred->sc_supp_groups = NULL;
}
return (0);
}
int
user_setcred(struct thread *td, const u_int flags,
const void *const uwcred, const size_t size, bool is_32bit)
{
struct setcred wcred;
#ifdef MAC
struct mac mac;
/* Pointer to 'struct mac' or 'struct mac32'. */
void *umac;
#endif
gid_t smallgroups[CRED_SMALLGROUPS_NB];
gid_t *groups = NULL;
int error;
/*
* As the only point of this wrapper function is to copyin() from
* userland, we only interpret the data pieces we need to perform this
* operation and defer further sanity checks to kern_setcred(), except
* that we redundantly check here that no unknown flags have been
* passed.
*/
if ((flags & ~SETCREDF_MASK) != 0)
return (EINVAL);
#ifdef COMPAT_FREEBSD32
if (is_32bit) {
struct setcred32 wcred32;
if (size != sizeof(wcred32))
return (EINVAL);
error = copyin(uwcred, &wcred32, sizeof(wcred32));
if (error != 0)
return (error);
/* These fields have exactly the same sizes and positions. */
memcpy(&wcred, &wcred32, &wcred32.setcred32_copy_end -
&wcred32.setcred32_copy_start);
/* Remaining fields are pointers and need PTRIN*(). */
PTRIN_CP(wcred32, wcred, sc_supp_groups);
PTRIN_CP(wcred32, wcred, sc_label);
} else
#endif /* COMPAT_FREEBSD32 */
{
if (size != sizeof(wcred))
return (EINVAL);
error = copyin(uwcred, &wcred, sizeof(wcred));
if (error != 0)
return (error);
}
#ifdef MAC
umac = wcred.sc_label;
#endif
/* Also done on !MAC as a defensive measure. */
wcred.sc_label = NULL;
/*
* Copy supplementary groups as needed. There is no specific
* alternative for 32-bit compatibility as 'gid_t' has the same size
* everywhere.
*/
error = kern_setcred_copyin_supp_groups(&wcred, flags, smallgroups,
&groups);
if (error != 0)
goto free_groups;
#ifdef MAC
if ((flags & SETCREDF_MAC_LABEL) != 0) {
#ifdef COMPAT_FREEBSD32
if (is_32bit)
error = mac_label_copyin32(umac, &mac, NULL);
else
#endif
error = mac_label_copyin(umac, &mac, NULL);
if (error != 0)
goto free_groups;
wcred.sc_label = &mac;
}
#endif
error = kern_setcred(td, flags, &wcred, groups);
#ifdef MAC
if (wcred.sc_label != NULL)
free_copied_label(wcred.sc_label);
#endif
free_groups:
if (groups != smallgroups)
free(groups, M_TEMP);
return (error);
}
#ifndef _SYS_SYSPROTO_H_
struct setcred_args {
u_int flags; /* Flags. */
const struct setcred *wcred;
size_t size; /* Passed 'setcred' structure length. */
};
#endif
/* ARGSUSED */
int
sys_setcred(struct thread *td, struct setcred_args *uap)
{
return (user_setcred(td, uap->flags, uap->wcred, uap->size, false));
}
/*
* CAUTION: This function normalizes groups in 'wcred'.
*
* If 'preallocated_groups' is non-NULL, it must be an already allocated array
* of size 'wcred->sc_supp_groups_nb + 1', with the supplementary groups
* starting at index 1, and 'wcred->sc_supp_groups' then must point to the first
* supplementary group.
*/
int
kern_setcred(struct thread *const td, const u_int flags,
struct setcred *const wcred, gid_t *preallocated_groups)
{
struct proc *const p = td->td_proc;
struct ucred *new_cred, *old_cred, *to_free_cred;
struct uidinfo *uip = NULL, *ruip = NULL;
#ifdef MAC
void *mac_set_proc_data = NULL;
bool proc_label_set = false;
#endif
gid_t *groups = NULL;
gid_t smallgroups[CRED_SMALLGROUPS_NB];
int error;
bool cred_set;
/* Bail out on unrecognized flags. */
if (flags & ~SETCREDF_MASK)
return (EINVAL);
/*
* Part 1: We allocate and perform preparatory operations with no locks.
*/
if (flags & SETCREDF_SUPP_GROUPS) {
if (wcred->sc_supp_groups_nb > ngroups_max)
return (EINVAL);
if (preallocated_groups != NULL) {
groups = preallocated_groups;
MPASS(preallocated_groups + 1 == wcred->sc_supp_groups);
} else {
groups = wcred->sc_supp_groups_nb < CRED_SMALLGROUPS_NB ?
smallgroups :
malloc((wcred->sc_supp_groups_nb + 1) *
sizeof(*groups), M_TEMP, M_WAITOK);
memcpy(groups + 1, wcred->sc_supp_groups,
wcred->sc_supp_groups_nb * sizeof(*groups));
}
}
if (flags & SETCREDF_MAC_LABEL) {
#ifdef MAC
error = mac_set_proc_prepare(td, wcred->sc_label,
&mac_set_proc_data);
if (error != 0)
goto free_groups;
#else
error = ENOTSUP;
goto free_groups;
#endif
}
if (flags & SETCREDF_UID) {
AUDIT_ARG_EUID(wcred->sc_uid);
uip = uifind(wcred->sc_uid);
}
if (flags & SETCREDF_RUID) {
AUDIT_ARG_RUID(wcred->sc_ruid);
ruip = uifind(wcred->sc_ruid);
}
if (flags & SETCREDF_SVUID)
AUDIT_ARG_SUID(wcred->sc_svuid);
if (flags & SETCREDF_GID)
AUDIT_ARG_EGID(wcred->sc_gid);
if (flags & SETCREDF_RGID)
AUDIT_ARG_RGID(wcred->sc_rgid);
if (flags & SETCREDF_SVGID)
AUDIT_ARG_SGID(wcred->sc_svgid);
if (flags & SETCREDF_SUPP_GROUPS) {
int ngrp = wcred->sc_supp_groups_nb;
/*
* Output the raw supplementary groups array for better
* traceability.
*/
AUDIT_ARG_GROUPSET(groups + 1, ngrp);
++ngrp;
groups_normalize(&ngrp, groups);
wcred->sc_supp_groups_nb = ngrp - 1;
}
/*
* We first completely build the new credentials and only then pass them
* to MAC along with the old ones so that modules can check whether the
* requested transition is allowed.
*/
new_cred = crget();
to_free_cred = new_cred;
if (flags & SETCREDF_SUPP_GROUPS)
crextend(new_cred, wcred->sc_supp_groups_nb + 1);
#ifdef MAC
mac_cred_setcred_enter();
#endif
/*
* Part 2: We grab the process lock as to have a stable view of its
* current credentials, and prepare a copy of them with the requested
* changes applied under that lock.
*/
PROC_LOCK(p);
old_cred = crcopysafe(p, new_cred);
/*
* Change user IDs.
*/
if (flags & SETCREDF_UID)
change_euid(new_cred, uip);
if (flags & SETCREDF_RUID)
change_ruid(new_cred, ruip);
if (flags & SETCREDF_SVUID)
change_svuid(new_cred, wcred->sc_svuid);
/*
* Change groups.
*
* crsetgroups_internal() changes both the effective and supplementary
* ones.
*/
if (flags & SETCREDF_SUPP_GROUPS) {
groups[0] = flags & SETCREDF_GID ? wcred->sc_gid :
new_cred->cr_gid;
crsetgroups_internal(new_cred, wcred->sc_supp_groups_nb + 1,
groups);
} else if (flags & SETCREDF_GID)
change_egid(new_cred, wcred->sc_gid);
if (flags & SETCREDF_RGID)
change_rgid(new_cred, wcred->sc_rgid);
if (flags & SETCREDF_SVGID)
change_svgid(new_cred, wcred->sc_svgid);
#ifdef MAC
/*
* Change the MAC label.
*/
if (flags & SETCREDF_MAC_LABEL) {
error = mac_set_proc_core(td, new_cred, mac_set_proc_data);
if (error != 0)
goto unlock_finish;
proc_label_set = true;
}
/*
* MAC security modules checks.
*/
error = mac_cred_check_setcred(flags, old_cred, new_cred);
if (error != 0)
goto unlock_finish;
#endif
/*
* Privilege check.
*/
error = priv_check_cred(old_cred, PRIV_CRED_SETCRED);
if (error != 0)
goto unlock_finish;
/*
* Set the new credentials, noting that they have changed.
*/
cred_set = proc_set_cred_enforce_proc_lim(p, new_cred);
if (cred_set) {
setsugid(p);
to_free_cred = old_cred;
MPASS(error == 0);
} else
error = EAGAIN;
unlock_finish:
PROC_UNLOCK(p);
/*
* Part 3: After releasing the process lock, we perform cleanups and
* finishing operations.
*/
#ifdef MAC
if (mac_set_proc_data != NULL)
mac_set_proc_finish(td, proc_label_set, mac_set_proc_data);
mac_cred_setcred_exit();
#endif
crfree(to_free_cred);
if (uip != NULL)
uifree(uip);
if (ruip != NULL)
uifree(ruip);
free_groups:
if (groups != preallocated_groups && groups != smallgroups)
free(groups, M_TEMP); /* Deals with 'groups' being NULL. */
return (error);
}
/*
* Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
* compatible. It says that setting the uid/gid to euid/egid is a special
* case of "appropriate privilege". Once the rules are expanded out, this
* basically means that setuid(nnn) sets all three id's, in all permitted
* cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
* does not set the saved id - this is dangerous for traditional BSD
* programs. For this reason, we *really* do not want to set
* _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
*/
#define POSIX_APPENDIX_B_4_2_2
#ifndef _SYS_SYSPROTO_H_
struct setuid_args {
uid_t uid;
};
#endif
/* ARGSUSED */
int
sys_setuid(struct thread *td, struct setuid_args *uap)
{
struct proc *p = td->td_proc;
struct ucred *newcred, *oldcred;
uid_t uid;
struct uidinfo *uip;
int error;
uid = uap->uid;
AUDIT_ARG_UID(uid);
newcred = crget();
uip = uifind(uid);
PROC_LOCK(p);
/*
* Copy credentials so other references do not see our changes.
*/
oldcred = crcopysafe(p, newcred);
#ifdef MAC
error = mac_cred_check_setuid(oldcred, uid);
if (error)
goto fail;
#endif
/*
* See if we have "permission" by POSIX 1003.1 rules.
*
* Note that setuid(geteuid()) is a special case of
* "appropriate privileges" in appendix B.4.2.2. We need
* to use this clause to be compatible with traditional BSD
* semantics. Basically, it means that "setuid(xx)" sets all
* three id's (assuming you have privs).
*
* Notes on the logic. We do things in three steps.
* 1: We determine if the euid is going to change, and do EPERM
* right away. We unconditionally change the euid later if this
* test is satisfied, simplifying that part of the logic.
* 2: We determine if the real and/or saved uids are going to
* change. Determined by compile options.
* 3: Change euid last. (after tests in #2 for "appropriate privs")
*/
if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
#ifdef _POSIX_SAVED_IDS
uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
#endif
#ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
#endif
(error = priv_check_cred(oldcred, PRIV_CRED_SETUID)) != 0)
goto fail;
#ifdef _POSIX_SAVED_IDS
/*
* Do we have "appropriate privileges" (are we root or uid == euid)
* If so, we are changing the real uid and/or saved uid.
*/
if (
#ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
uid == oldcred->cr_uid ||
#endif
/* We are using privs. */
priv_check_cred(oldcred, PRIV_CRED_SETUID) == 0)
#endif
{
/*
* Set the real uid.
*/
if (uid != oldcred->cr_ruid) {
change_ruid(newcred, uip);
setsugid(p);
}
/*
* Set saved uid
*
* XXX always set saved uid even if not _POSIX_SAVED_IDS, as
* the security of seteuid() depends on it. B.4.2.2 says it
* is important that we should do this.
*/
if (uid != oldcred->cr_svuid) {
change_svuid(newcred, uid);
setsugid(p);
}
}
/*
* In all permitted cases, we are changing the euid.
*/
if (uid != oldcred->cr_uid) {
change_euid(newcred, uip);
setsugid(p);
}
/*
* This also transfers the proc count to the new user.
*/
proc_set_cred(p, newcred);
#ifdef RACCT
racct_proc_ucred_changed(p, oldcred, newcred);
crhold(newcred);
#endif
PROC_UNLOCK(p);
#ifdef RCTL
rctl_proc_ucred_changed(p, newcred);
crfree(newcred);
#endif
uifree(uip);
crfree(oldcred);
return (0);
fail:
PROC_UNLOCK(p);
uifree(uip);
crfree(newcred);
return (error);
}
#ifndef _SYS_SYSPROTO_H_
struct seteuid_args {
uid_t euid;
};
#endif
/* ARGSUSED */
int
sys_seteuid(struct thread *td, struct seteuid_args *uap)
{
struct proc *p = td->td_proc;
struct ucred *newcred, *oldcred;
uid_t euid;
struct uidinfo *euip;
int error;
euid = uap->euid;