-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcredentials.cpp
627 lines (525 loc) · 19.5 KB
/
credentials.cpp
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
/*
Fakeroot Next Generation - run command with fake root privileges
This program is copyrighted. Copyright information is available at the
AUTHORS file at the root of the source tree for the fakeroot-ng project
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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
*/
#include "config.h"
#include <errno.h>
#include "syscalls.h"
#include "arch/platform.h"
bool sys_getuid( int sc_num, pid_t pid, pid_state *state )
{
switch( state->state ) {
default:
case pid_state::NONE:
state->state=pid_state::RETURN;
break;
case pid_state::RETURN:
ptlib_set_retval( pid, state->uid );
state->state=pid_state::NONE;
break;
}
return true;
}
bool sys_geteuid( int sc_num, pid_t pid, pid_state *state )
{
switch( state->state ) {
default:
case pid_state::NONE:
state->state=pid_state::RETURN;
break;
case pid_state::RETURN:
ptlib_set_retval( pid, state->euid );
state->state=pid_state::NONE;
break;
}
return true;
}
bool sys_getgid( int sc_num, pid_t pid, pid_state *state )
{
switch( state->state ) {
default:
case pid_state::NONE:
state->state=pid_state::RETURN;
break;
case pid_state::RETURN:
ptlib_set_retval( pid, state->gid );
state->state=pid_state::NONE;
break;
}
return true;
}
bool sys_getegid( int sc_num, pid_t pid, pid_state *state )
{
switch( state->state ) {
default:
case pid_state::NONE:
state->state=pid_state::RETURN;
break;
case pid_state::RETURN:
ptlib_set_retval( pid, state->egid );
state->state=pid_state::NONE;
break;
}
return true;
}
#ifdef SYS_getresuid
bool sys_getresuid( int sc_num, pid_t pid, pid_state *state )
{
switch( state->state ) {
default:
case pid_state::NONE:
state->state=pid_state::RETURN;
// Do not trust the syscall not to change the pointers
state->context_state[0]=ptlib_get_argument( pid, 1 );
state->context_state[1]=ptlib_get_argument( pid, 2 );
state->context_state[2]=ptlib_get_argument( pid, 3 );
break;
case pid_state::RETURN:
if( ptlib_success(pid, sc_num) ) {
bool success;
success=ptlib_set_mem( pid, &state->uid, state->context_state[0], sizeof(state->uid) );
success=success && ptlib_set_mem( pid, &state->euid, state->context_state[1], sizeof(state->euid) );
success=success && ptlib_set_mem( pid, &state->suid, state->context_state[2], sizeof(state->suid) );
if( !success ) {
ptlib_set_error( pid, state->orig_sc, errno );
}
}
state->state=pid_state::NONE;
break;
}
return true;
}
#endif
#ifdef SYS_getresgid
bool sys_getresgid( int sc_num, pid_t pid, pid_state *state )
{
switch( state->state ) {
default:
case pid_state::NONE:
state->state=pid_state::RETURN;
// Do not trust the syscall not to change the pointers
state->context_state[0]=ptlib_get_argument( pid, 1 );
state->context_state[1]=ptlib_get_argument( pid, 2 );
state->context_state[2]=ptlib_get_argument( pid, 3 );
break;
case pid_state::RETURN:
if( ptlib_success(pid, sc_num) ) {
bool success;
success=ptlib_set_mem( pid, &state->gid, state->context_state[0], sizeof(state->gid) );
success=success && ptlib_set_mem( pid, &state->egid, state->context_state[1], sizeof(state->egid) );
success=success && ptlib_set_mem( pid, &state->sgid, state->context_state[2], sizeof(state->sgid) );
if( !success ) {
ptlib_set_error( pid, state->orig_sc, errno );
}
}
state->state=pid_state::NONE;
break;
}
return true;
}
#endif
bool sys_getgroups( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
ptlib_set_syscall(pid, PREF_NOP);
state->state=pid_state::REDIRECT2;
// Store the arguments for later
state->context_state[0]=ptlib_get_argument( pid, 1 );
state->context_state[1]=ptlib_get_argument( pid, 2 );
} else if( state->state==pid_state::REDIRECT2 ) {
state->state=pid_state::NONE;
// What is the size?
if( state->context_state[0]==0 ) {
// Merely report the number of groups we have
ptlib_set_retval( pid, state->groups.size() );
} else if( state->context_state[0]<state->groups.size() ) {
// Not enough room
ptlib_set_error( pid, state->orig_sc, EINVAL );
} else {
unsigned int count=0;
gid_t *groups=(gid_t *)state->context_state[1];
bool success=true;
for( std::set<gid_t>::const_iterator i=state->groups.begin(); success && i!=state->groups.end(); ++i, ++count ) {
success=ptlib_set_mem( pid, &*i, (int_ptr)(groups+count), sizeof(gid_t) );
}
if( success )
ptlib_set_retval( pid, count );
else
ptlib_set_error( pid, state->orig_sc, errno );
}
}
return true;
}
bool sys_setuid( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
// Do we let the syscall proceed?
state->context_state[0]=ptlib_get_argument( pid, 1 );
// NOP the actual call
ptlib_set_syscall( pid, PREF_NOP );
state->state=pid_state::REDIRECT2;
} else if( state->state==pid_state::REDIRECT2 ) {
// Let's see if we want to perform the action
uid_t uid=(uid_t)state->context_state[0];
if( state->euid==ROOT_UID ) {
// Super user version
state->uid=state->euid=state->suid=state->fsuid=uid;
ptlib_set_retval( pid, 0 );
} else if( state->uid==uid || state->suid==uid ) {
// Regular user, but with an operation that is ok
state->euid=state->fsuid=uid;
ptlib_set_retval (pid, 0 );
} else {
// No permission
ptlib_set_error( pid, state->orig_sc, EPERM );
}
state->state=pid_state::NONE;
}
return true;
}
static bool check_uids( uid_t new_uid, const pid_state *state )
{
return state->euid==ROOT_UID || new_uid==state->uid || new_uid==state->euid || new_uid==state->suid;
}
bool sys_seteuid( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
// Do we let the syscall proceed?
state->context_state[0]=ptlib_get_argument( pid, 1 );
// NOP the actual call
ptlib_set_syscall( pid, PREF_NOP );
state->state=pid_state::REDIRECT2;
} else if( state->state==pid_state::REDIRECT2 ) {
// Let's see if we want to perform the action
uid_t uid=(uid_t)state->context_state[0];
if( check_uids( uid, state ) ) {
state->euid=state->fsuid=uid;
ptlib_set_retval( pid, 0 );
} else {
// No permission
ptlib_set_error( pid, state->orig_sc, EPERM );
}
state->state=pid_state::NONE;
}
return true;
}
bool sys_setfsuid( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
// Do we let the syscall proceed?
state->context_state[0]=ptlib_get_argument( pid, 1 );
// NOP the actual call
ptlib_set_syscall( pid, PREF_NOP );
state->state=pid_state::REDIRECT2;
} else if( state->state==pid_state::REDIRECT2 ) {
// Let's see if we want to perform the action
uid_t uid=(uid_t)state->context_state[0];
uid_t old_fsuid=state->fsuid;
if( check_uids( uid, state ) ) {
state->fsuid=uid;
} else {
// No permission - nothing to do
}
ptlib_set_retval( pid, old_fsuid );
state->state=pid_state::NONE;
}
return true;
}
bool sys_setresuid( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
// Do we let the syscall proceed?
state->context_state[0]=ptlib_get_argument( pid, 1 ); //ruid
state->context_state[1]=ptlib_get_argument( pid, 2 ); //euid
state->context_state[2]=ptlib_get_argument( pid, 3 ); //suid
// NOP the actual call
ptlib_set_syscall( pid, PREF_NOP );
state->state=pid_state::REDIRECT2;
} else if( state->state==pid_state::REDIRECT2 ) {
// Let's see if we want to perform the action
bool success=true;
uid_t new_uid=state->uid, new_euid=state->euid, new_suid=state->suid;
if( state->context_state[0]!=(int_ptr)-1 ) {
if( check_uids( state->context_state[0], state ) )
new_uid=state->context_state[0];
else
success=false;
}
if( state->context_state[1]!=(int_ptr)-1 ) {
if( success && check_uids( state->context_state[1], state ) )
new_euid=state->context_state[1];
else
success=false;
}
if( state->context_state[2]!=(int_ptr)-1 ) {
if( success && check_uids( state->context_state[2], state ) )
new_suid=state->context_state[2];
else
success=false;
}
// If all checks passed, commit the changes
if( success ) {
state->uid=new_uid;
state->euid=new_euid;
state->suid=new_suid;
ptlib_set_retval( pid, 0 );
} else {
ptlib_set_error( pid, state->orig_sc, EPERM );
}
state->state=pid_state::NONE;
}
return true;
}
bool sys_setreuid( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
// Do we let the syscall proceed?
state->context_state[0]=ptlib_get_argument( pid, 1 ); //ruid
state->context_state[1]=ptlib_get_argument( pid, 2 ); //euid
// NOP the actual call
ptlib_set_syscall( pid, PREF_NOP );
state->state=pid_state::REDIRECT2;
} else if( state->state==pid_state::REDIRECT2 ) {
// Let's see if we want to perform the action
bool success=true;
uid_t new_uid=state->uid, new_euid=state->euid, new_suid=state->suid;
if( state->context_state[0]!=(int_ptr)-1 ) {
// XXX Linux specific behavior
if( state->euid==ROOT_UID || state->context_state[0]==state->uid || state->context_state[0]==state->suid )
new_uid=state->context_state[0];
else
success=false;
}
if( state->context_state[1]!=(int_ptr)-1 ) {
if( success && check_uids( state->context_state[1], state ) )
new_euid=state->context_state[1];
else
success=false;
}
// There is no POSIX documentation on what should happen to the saved UID. The following is the Linux logic
if( success && (state->context_state[0]!=(int_ptr)-1 ||
( state->context_state[1]!=(int_ptr)-1 && state->context_state[1]!=state->uid )) )
{
new_suid=new_euid;
}
// If all checks passed, commit the changes
if( success ) {
state->uid=new_uid;
state->euid=state->fsuid=new_euid;
state->suid=new_suid;
ptlib_set_retval( pid, 0 );
} else {
ptlib_set_error( pid, state->orig_sc, EPERM );
}
state->state=pid_state::NONE;
}
return true;
}
bool sys_setgroups( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
ptlib_set_syscall(pid, PREF_NOP);
state->state=pid_state::REDIRECT2;
// Store the arguments for later
state->context_state[0]=ptlib_get_argument( pid, 1 );
state->context_state[1]=ptlib_get_argument( pid, 2 );
} else if( state->state==pid_state::REDIRECT2 ) {
state->state=pid_state::NONE;
std::set<gid_t> new_groups;
gid_t *process_groups=(gid_t *)state->context_state[1];
int error=0;
while( error==0 && state->context_state[0]>0 ) {
gid_t group;
if( ptlib_get_mem( pid, (int_ptr)process_groups++, &group, sizeof(gid_t) ) ) {
new_groups.insert(group);
--state->context_state[0];
} else {
error=errno;
}
}
if( error==0 ) {
state->groups=new_groups;
ptlib_set_retval( pid, 0 );
} else {
ptlib_set_error( pid, state->orig_sc, error );
}
}
return true;
}
bool sys_setgid( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
// Do we let the syscall proceed?
state->context_state[0]=ptlib_get_argument( pid, 1 );
// NOP the actual call
ptlib_set_syscall( pid, PREF_NOP );
state->state=pid_state::REDIRECT2;
} else if( state->state==pid_state::REDIRECT2 ) {
// Let's see if we want to perform the action
gid_t gid=(gid_t)state->context_state[0];
if( state->egid==ROOT_UID ) {
// Super user version
state->gid=state->egid=state->sgid=state->fsgid=gid;
ptlib_set_retval( pid, 0 );
} else if( state->gid==gid || state->sgid==gid ) {
// Regular user, but with an operation that is ok
state->egid=state->fsgid=gid;
ptlib_set_retval (pid, 0 );
} else {
// No permission
ptlib_set_error( pid, state->orig_sc, EPERM );
}
state->state=pid_state::NONE;
}
return true;
}
static bool check_gids( gid_t new_gid, const pid_state *state )
{
return state->egid==ROOT_GID || new_gid==state->gid || new_gid==state->egid || new_gid==state->sgid;
}
bool sys_setegid( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
// Save the original arguments
state->context_state[0]=ptlib_get_argument( pid, 1 );
// NOP the actual call
ptlib_set_syscall( pid, PREF_NOP );
state->state=pid_state::REDIRECT2;
} else if( state->state==pid_state::REDIRECT2 ) {
// Let's see if we want to perform the action
gid_t gid=(gid_t)state->context_state[0];
if( check_gids( gid, state ) ) {
state->egid=state->fsgid=gid;
ptlib_set_retval( pid, 0 );
} else {
// No permission
ptlib_set_error( pid, state->orig_sc, EPERM );
}
state->state=pid_state::NONE;
}
return true;
}
bool sys_setfsgid( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
// Do we let the syscall proceed?
state->context_state[0]=ptlib_get_argument( pid, 1 );
// NOP the actual call
ptlib_set_syscall( pid, PREF_NOP );
state->state=pid_state::REDIRECT2;
} else if( state->state==pid_state::REDIRECT2 ) {
// Let's see if we want to perform the action
gid_t gid=(gid_t)state->context_state[0];
gid_t old_fsgid=state->fsgid;
if( check_gids( gid, state ) ) {
state->fsgid=gid;
} else {
// No permission - nothing to do
}
ptlib_set_retval( pid, old_fsgid );
state->state=pid_state::NONE;
}
return true;
}
bool sys_setresgid( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
// Do we let the syscall proceed?
state->context_state[0]=ptlib_get_argument( pid, 1 ); //rgid
state->context_state[1]=ptlib_get_argument( pid, 2 ); //egid
state->context_state[2]=ptlib_get_argument( pid, 3 ); //sgid
// NOP the actual call
ptlib_set_syscall( pid, PREF_NOP );
state->state=pid_state::REDIRECT2;
} else if( state->state==pid_state::REDIRECT2 ) {
// Let's see if we want to perform the action
bool success=true;
gid_t new_gid=state->gid, new_egid=state->egid, new_sgid=state->sgid;
if( state->context_state[0]!=(int_ptr)-1 ) {
if( check_gids( state->context_state[0], state ) )
new_gid=state->context_state[0];
else
success=false;
}
if( state->context_state[1]!=(int_ptr)-1 ) {
if( success && check_gids( state->context_state[1], state ) )
new_egid=state->context_state[1];
else
success=false;
}
if( state->context_state[2]!=(int_ptr)-1 ) {
if( success && check_gids( state->context_state[2], state ) )
new_sgid=state->context_state[2];
else
success=false;
}
// If all checks passed, commit the changes
if( success ) {
state->gid=new_gid;
state->egid=new_egid;
state->sgid=new_sgid;
ptlib_set_retval( pid, 0 );
} else {
ptlib_set_error( pid, state->orig_sc, EPERM );
}
state->state=pid_state::NONE;
}
return true;
}
bool sys_setregid( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
// Do we let the syscall proceed?
state->context_state[0]=ptlib_get_argument( pid, 1 ); //rgid
state->context_state[1]=ptlib_get_argument( pid, 2 ); //egid
// NOP the actual call
ptlib_set_syscall( pid, PREF_NOP );
state->state=pid_state::REDIRECT2;
} else if( state->state==pid_state::REDIRECT2 ) {
// Let's see if we want to perform the action
bool success=true;
gid_t new_gid=state->gid, new_egid=state->egid, new_sgid=state->sgid;
if( state->context_state[0]!=(int_ptr)-1 ) {
// XXX Linux specific behavior
if( state->egid==ROOT_GID || state->context_state[0]==state->gid || state->context_state[0]==state->sgid )
new_gid=state->context_state[0];
else
success=false;
}
if( state->context_state[1]!=(int_ptr)-1 ) {
if( success && check_gids( state->context_state[1], state ) )
new_egid=state->context_state[1];
else
success=false;
}
// There is no POSIX documentation on what should happen to the saved UID. The following is the Linux logic
if( success && (state->context_state[0]!=(int_ptr)-1 ||
( state->context_state[1]!=(int_ptr)-1 && state->context_state[1]!=state->gid )) )
{
new_sgid=new_egid;
}
// If all checks passed, commit the changes
if( success ) {
state->gid=new_gid;
state->egid=state->fsgid=new_egid;
state->sgid=new_sgid;
ptlib_set_retval( pid, 0 );
} else {
ptlib_set_error( pid, state->orig_sc, EPERM );
}
state->state=pid_state::NONE;
}
return true;
}