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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
|
/*
* Part of Very Secure FTPd
* Licence: GPL v2
* Author: Chris Evans
* ptracesandbox.c
*
* Generic routines to setup and run a process under a restrictive ptrace()
* based sandbox.
* Note that the style in this file is to not go via the helper functions in
* sysutil.c, but instead hit the system APIs directly. This is because I may
* very well release just this file to the public domain, and do not want
* dependencies on other parts of vsftpd.
*/
#include "ptracesandbox.h"
#if defined(__linux__) && defined(__i386__)
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/ptrace.h>
/* For AF_MAX (NPROTO is defined to this) */
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <asm/unistd.h>
#ifndef __NR_sendfile64
#define __NR_sendfile64 239
#endif
#ifndef __NR_exit_group
#define __NR_exit_group 252
#endif
#ifndef __NR_utimes
#define __NR_utimes 271
#endif
/* For the socketcall() multiplex args. */
#include <linux/net.h>
#ifndef PTRACE_SETOPTIONS
#define PTRACE_SETOPTIONS 0x4200
#endif
#ifndef PTRACE_O_TRACESYSGOOD
#define PTRACE_O_TRACESYSGOOD 1
#endif
#ifndef PTRACE_O_TRACEFORK
#define PTRACE_O_TRACEFORK 2
#endif
#ifndef PTRACE_O_TRACEVFORK
#define PTRACE_O_TRACEVFORK 4
#endif
#ifndef PTRACE_O_TRACECLONE
#define PTRACE_O_TRACECLONE 8
#endif
#ifndef O_DIRECT
#define O_DIRECT 040000
#endif
static void sanitize_child();
static int get_action(struct pt_sandbox* p_sandbox);
static int validate_mmap2(struct pt_sandbox* p_sandbox, void* p_arg);
static int validate_open_default(struct pt_sandbox* p_sandbox, void* p_arg);
static int validate_open_readonly(struct pt_sandbox* p_sandbox, void* p_arg);
static int validate_fcntl(struct pt_sandbox* p_sandbox, void* p_arg);
static int validate_socketcall(struct pt_sandbox* p_sandbox, void* p_arg);
static void install_socketcall(struct pt_sandbox* p_sandbox);
#define MAX_SYSCALL 300
struct pt_sandbox
{
int read_event_fd;
int write_event_fd;
pid_t pid;
int is_allowed[MAX_SYSCALL];
ptrace_sandbox_validator_t validator[MAX_SYSCALL];
void* validator_arg[MAX_SYSCALL];
int is_exit;
struct user_regs_struct regs;
int is_socketcall_allowed[NPROTO];
ptrace_sandbox_validator_t socketcall_validator[NPROTO];
void* socketcall_validator_arg[NPROTO];
};
static int s_sigchld_fd = -1;
void
handle_sigchld(int sig)
{
int ret;
if (sig != SIGCHLD)
{
_exit(1);
}
if (s_sigchld_fd != -1)
{
do
{
static const char zero = '\0';
ret = write(s_sigchld_fd, &zero, sizeof(zero));
} while (ret == -1 && errno == EINTR);
if (ret != 1)
{
_exit(2);
}
}
}
struct pt_sandbox*
ptrace_sandbox_alloc()
{
int i;
struct sigaction sigact;
struct pt_sandbox* ret = malloc(sizeof(struct pt_sandbox));
if (ret == NULL)
{
return NULL;
}
ret->pid = -1;
ret->read_event_fd = -1;
ret->write_event_fd = -1;
ret->is_exit = 0;
memset(&ret->regs, '\0', sizeof(ret->regs));
for (i = 0; i < MAX_SYSCALL; ++i)
{
ret->is_allowed[i] = 0;
ret->validator[i] = 0;
ret->validator_arg[i] = 0;
}
for (i = 0; i < NPROTO; ++i)
{
ret->is_socketcall_allowed[i] = 0;
ret->socketcall_validator[i] = 0;
ret->socketcall_validator_arg[i] = 0;
}
memset((void*) &sigact, '\0', sizeof(sigact));
sigact.sa_handler = handle_sigchld;
if (sigaction(SIGCHLD, &sigact, NULL) != 0)
{
goto err_out;
}
return ret;
err_out:
ptrace_sandbox_free(ret);
return NULL;
}
void
ptrace_sandbox_free(struct pt_sandbox* p_sandbox)
{
if (p_sandbox->pid != -1)
{
warnx("bug: pid active in ptrace_sandbox_free");
/* We'll kill it for you so it doesn't escape the sandbox totally, but
* we won't reap the zombie.
* Killing it like this is a risk: if it's stopped in syscall entry,
* that syscall will execute before the pending kill takes effect.
* If that pending syscall were to be a fork(), there could be trouble.
*/
(void) kill(p_sandbox->pid, SIGKILL);
}
if (p_sandbox->read_event_fd != -1)
{
s_sigchld_fd = -1;
close(p_sandbox->read_event_fd);
close(p_sandbox->write_event_fd);
}
free(p_sandbox);
}
void
ptrace_sandbox_attach_point()
{
long pt_ret;
int ret;
pid_t pid = getpid();
if (pid <= 1)
{
warnx("weird pid");
_exit(1);
}
/* You don't have to use PTRACE_TRACEME, but if you don't, a rogue SIGCONT
* might wake you up from the STOP below before the tracer has attached.
*/
pt_ret = ptrace(PTRACE_TRACEME, 0, 0, 0);
if (pt_ret != 0)
{
warn("PTRACE_TRACEME failed");
_exit(2);
}
ret = kill(pid, SIGSTOP);
if (ret != 0)
{
warn("kill SIGSTOP failed");
_exit(3);
}
}
int
ptrace_sandbox_launch_process(struct pt_sandbox* p_sandbox,
void (*p_func)(void*),
void* p_arg)
{
long pt_ret;
pid_t ret;
int status;
if (p_sandbox->pid != -1)
{
warnx("bug: process already active");
return -1;
}
ret = fork();
if (ret < 0)
{
return -1;
}
else if (ret == 0)
{
/* Child context. */
sanitize_child();
(*p_func)(p_arg);
_exit(0);
}
/* Parent context */
p_sandbox->pid = ret;
do
{
ret = waitpid(p_sandbox->pid, &status, 0);
} while (ret == -1 && errno == EINTR);
if (ret == -1)
{
warn("waitpid failed");
goto kill_out;
}
else if (ret != p_sandbox->pid)
{
warnx("unknown pid %d", ret);
goto kill_out;
}
if (!WIFSTOPPED(status))
{
warnx("not stopped status %d\n", status);
goto kill_out;
}
if (WSTOPSIG(status) != SIGSTOP)
{
warnx("not SIGSTOP status %d\n", status);
goto kill_out;
}
/* The fork, etc. tracing options are worth a bit of explanation. We don't
* permit process launching syscalls at all as they are dangerous. But
* there's a small race if the untrusted process attempts a denied fork()
* and then takes a rouge SIGKILL before the supervisor gets a chance to
* clear the orig_eax register. In this case the syscall will still execute.
* (Policies may not include signal sending capabilities, thus mitigating this
* direct attack, however a rogue SIGKILL may come from a non-malicious
* source). Therefore, we'd rather any fork()ed process starts off traced,
* just in case this tiny race condition triggers.
*/
pt_ret = ptrace(PTRACE_SETOPTIONS,
p_sandbox->pid,
0,
PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE);
if (pt_ret != 0)
{
warn("PTRACE_SETOPTIONS failure");
goto kill_out;
}
return p_sandbox->pid;
kill_out:
(void) kill(p_sandbox->pid, SIGKILL);
p_sandbox->pid = -1;
return -1;
}
int
ptrace_sandbox_continue_process(struct pt_sandbox* p_sandbox, int sig)
{
long pt_ret = ptrace(PTRACE_SYSCALL, p_sandbox->pid, 0, sig);
if (pt_ret != 0)
{
warn("PTRACE_SYSCALL failure");
if (errno == ESRCH)
{
return PTRACE_SANDBOX_ERR_DEAD;
}
return PTRACE_SANDBOX_ERR_PTRACE;
}
return 0;
}
int
ptrace_sandbox_get_event_fd(struct pt_sandbox* p_sandbox)
{
/* TODO: allocate pipe fds */
(void) p_sandbox;
return -1;
}
int
ptrace_sandbox_get_event(struct pt_sandbox* p_sandbox, int* status, int block)
{
pid_t pid;
int options = 0;
if (!block)
{
options = WNOHANG;
}
do
{
pid = waitpid(p_sandbox->pid, status, options);
} while (pid == -1 && errno == EINTR);
if (pid == -1)
{
warn("waitpid failure");
if (errno == ECHILD)
{
return PTRACE_SANDBOX_ERR_DEAD;
}
return PTRACE_SANDBOX_ERR_WAITPID;
}
return pid;
}
int
ptrace_sandbox_handle_event(struct pt_sandbox* p_sandbox, int status)
{
int sig;
int action;
if (WIFEXITED(status) || WIFSIGNALED(status))
{
p_sandbox->pid = -1;
return 1;
}
if (!WIFSTOPPED(status))
{
warnx("weird status: %d\n", status);
return PTRACE_SANDBOX_ERR_WAIT_STATUS;
}
sig = WSTOPSIG(status);
if (sig >= 0 && sig < 0x80)
{
/* It's a normal signal; deliver it right on. SIGSTOP / SIGCONT handling
* are buggy in the kernel and I'm not sure it's safe to pass either on,
* so the signal becomes a little more... robust :)
*/
if (sig == SIGSTOP || sig == SIGCONT)
{
sig = SIGKILL;
}
return ptrace_sandbox_continue_process(p_sandbox, sig);
}
if (!(sig & 0x80))
{
warnx("weird status: %d\n", status);
return PTRACE_SANDBOX_ERR_WAIT_STATUS;
}
/* Syscall trap. */
if (p_sandbox->is_exit)
{
p_sandbox->is_exit = 0;
}
else
{
p_sandbox->is_exit = 1;
action = get_action(p_sandbox);
if (action != 0)
{
return action;
}
}
return ptrace_sandbox_continue_process(p_sandbox, 0);
}
int
ptrace_sandbox_run_processes(struct pt_sandbox* p_sandbox)
{
if (ptrace_sandbox_continue_process(p_sandbox, 0) != 0)
{
goto kill_out;
}
while (1)
{
int status;
int ret = ptrace_sandbox_get_event(p_sandbox, &status, 1);
if (ret <= 0)
{
goto kill_out;
}
ret = ptrace_sandbox_handle_event(p_sandbox, status);
if (ret < 0)
{
warnx("couldn't handle sandbox event");
goto kill_out;
}
if (ret == 1)
{
return 0;
}
}
kill_out:
ptrace_sandbox_kill_processes(p_sandbox);
return -1;
}
void
ptrace_sandbox_kill_processes(struct pt_sandbox* p_sandbox)
{
long pt_ret;
struct user_regs_struct regs;
pid_t pid = p_sandbox->pid;
if (pid == -1)
{
return;
}
p_sandbox->pid = -1;
pt_ret = ptrace(PTRACE_GETREGS, pid, 0, ®s);
if (pt_ret != 0)
{
warn("PTRACE_GETREGS failure");
/* This API is supposed to be called with the process stopped; but if it
* is still running, we can at least help a bit. See security related
* comment in ptrace_sandbox_free(), though.
*/
(void) kill(pid, SIGKILL);
return;
}
/* Kind of nasty, but the only way of stopping a started syscall from
* executing is to rewrite the registers to execute a different syscall.
*/
regs.orig_eax = __NR_exit_group;
regs.eip = 0xffffffff;
pt_ret = ptrace(PTRACE_SETREGS, pid, 0, ®s);
if (pt_ret != 0)
{
warn("PTRACE_SETREGS failure");
/* Deliberate fall-thru. */
}
pt_ret = ptrace(PTRACE_KILL, pid, 0, 0);
if (pt_ret != 0)
{
warn("PTRACE_KILL failure");
/* Deliberate fall-thru. */
}
/* Just to make ourselves clear. */
(void) kill(pid, SIGKILL);
/* So the GETREGS succeeded, so the process definitely _was_ there. We can
* safely wait for it to reap the zombie.
*/
(void) waitpid(pid, NULL, 0);
}
int
ptrace_sandbox_get_arg(struct pt_sandbox* p_sandbox,
int arg,
unsigned long* p_out)
{
long ret = 0;
struct user_regs_struct* p_regs = &p_sandbox->regs;
if (p_regs->orig_eax == 0)
{
return PTRACE_SANDBOX_ERR_API_ABUSE_STOPIT;
}
if (arg < 0 || arg > 5)
{
return PTRACE_SANDBOX_ERR_API_ABUSE_STOPIT;
}
switch (arg)
{
case 0:
ret = p_regs->ebx;
break;
case 1:
ret = p_regs->ecx;
break;
case 2:
ret = p_regs->edx;
break;
case 3:
ret = p_regs->esi;
break;
case 4:
ret = p_regs->edi;
break;
case 5:
ret = p_regs->ebp;
break;
}
*p_out = ret;
return 0;
}
int
ptrace_sandbox_get_socketcall_arg(struct pt_sandbox* p_sandbox,
int arg,
unsigned long* p_out)
{
unsigned long ptr;
int ret;
struct user_regs_struct* p_regs = &p_sandbox->regs;
if (p_regs->orig_eax == 0)
{
return PTRACE_SANDBOX_ERR_API_ABUSE_STOPIT;
}
if (arg < 0 || arg > 2)
{
return PTRACE_SANDBOX_ERR_API_ABUSE_STOPIT;
}
ret = ptrace_sandbox_get_arg(p_sandbox, 1, &ptr);
if (ret != 0)
{
return ret;
}
ptr += (arg * 4);
ret = ptrace_sandbox_get_long(p_sandbox, ptr, p_out);
return ret;
}
int
ptrace_sandbox_get_long(struct pt_sandbox* p_sandbox,
unsigned long ptr,
unsigned long* p_out)
{
return ptrace_sandbox_get_buf(p_sandbox, ptr, sizeof(long), (void*) p_out);
}
int
ptrace_sandbox_get_buf(struct pt_sandbox* p_sandbox,
unsigned long ptr,
unsigned long len,
void* p_buf)
{
long pt_ret;
char* p_out = (char*) p_buf;
for (; len > 0; len -= sizeof(long))
{
errno = 0;
pt_ret = ptrace(PTRACE_PEEKDATA, p_sandbox->pid, (void*) ptr, 0);
if (pt_ret == -1 && errno != 0)
{
warn("PTRACE_GETREGS failure");
if (errno == ESRCH)
{
return PTRACE_SANDBOX_ERR_DEAD;
}
return PTRACE_SANDBOX_ERR_PTRACE;
}
if (len >= sizeof(long))
{
memcpy(p_out, &pt_ret, sizeof(long));
}
else
{
memcpy(p_out, &pt_ret, len);
}
p_out += sizeof(long);
ptr += sizeof(long);
}
return 0;
}
static void
sanitize_child()
{
/* Ensure that if our sandbox supervisor goes down, so do we. */
int ret = prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
if (ret != 0)
{
_exit(3);
}
}
static int
get_action(struct pt_sandbox* p_sandbox)
{
int ret;
int call;
int cs;
long pt_ret = ptrace(PTRACE_GETREGS, p_sandbox->pid, 0, &(p_sandbox->regs));
if (pt_ret != 0)
{
warn("PTRACE_GETREGS failure");
if (errno == ESRCH)
{
return PTRACE_SANDBOX_ERR_DEAD;
}
return PTRACE_SANDBOX_ERR_PTRACE;
}
/* We need to be sure that the child is attempting a syscall against the
* 32-bit syscall table, otherwise they can bypass the policy by abusing the
* fact that e.g. syscall 200 is getgid32() on 32-bit but tkill() on 64-bit.
* If the syscall instruct was int80 or sysenter, is it guaranteed to hit
* the 32-bit table. If it is syscall, the current CS selector determines
* the table. Therefore, we can check the current CS selector references a
* known system-only selector that is guaranteed 32-bit (not long mode).
*/
cs = p_sandbox->regs.xcs;
if (cs != 0x73 && cs != 0x23)
{
warnx("bad CS %d", cs);
ret = PTRACE_SANDBOX_ERR_BAD_SYSCALL;
goto out;
}
call = (int) p_sandbox->regs.orig_eax;
if (call < 0 || call >= MAX_SYSCALL)
{
warnx("syscall %d out of bounds", call);
ret = PTRACE_SANDBOX_ERR_BAD_SYSCALL;
goto out;
}
if (p_sandbox->is_allowed[call] != 1)
{
syslog(LOG_LOCAL0 | LOG_DEBUG, "syscall not permitted: %d", call);
warnx("syscall not permitted: %d", call);
ret = PTRACE_SANDBOX_ERR_POLICY_SYSCALL;
goto out;
}
if (p_sandbox->validator[call])
{
ptrace_sandbox_validator_t p_validate = p_sandbox->validator[call];
int validate_ret = (*p_validate)(p_sandbox, p_sandbox->validator_arg[call]);
if (validate_ret != 0)
{
syslog(LOG_LOCAL0 | LOG_DEBUG,
"syscall validate fail: %d (%d)",
call,
validate_ret);
warnx("syscall validate failed: %d (%d)", call, validate_ret);
ret = PTRACE_SANDBOX_ERR_POLICY_ARGS;
goto out;
}
}
ret = 0;
out:
memset(&p_sandbox->regs, '\0', sizeof(p_sandbox->regs));
return ret;
}
void
ptrace_sandbox_permit_exit(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_exit] = 1;
p_sandbox->is_allowed[__NR_exit_group] = 1;
}
void
ptrace_sandbox_permit_read(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_read] = 1;
}
void
ptrace_sandbox_permit_write(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_write] = 1;
}
void
ptrace_sandbox_permit_sigaction(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_sigaction] = 1;
p_sandbox->is_allowed[__NR_rt_sigaction] = 1;
}
void
ptrace_sandbox_permit_alarm(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_alarm] = 1;
}
void
ptrace_sandbox_permit_query_time(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_gettimeofday] = 1;
p_sandbox->is_allowed[__NR_time] = 1;
}
void
ptrace_sandbox_permit_mmap(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_mmap2] = 1;
p_sandbox->validator[__NR_mmap2] = validate_mmap2;
}
static int
validate_mmap2(struct pt_sandbox* p_sandbox, void* p_arg)
{
unsigned long arg4;
int ret = ptrace_sandbox_get_arg(p_sandbox, 3, &arg4);
(void) p_arg;
if (ret != 0)
{
return ret;
}
if (arg4 & MAP_SHARED)
{
return -1;
}
return 0;
}
void
ptrace_sandbox_permit_mprotect(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_mprotect] = 1;
}
void
ptrace_sandbox_permit_file_stats(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_stat] = 1;
p_sandbox->is_allowed[__NR_stat64] = 1;
p_sandbox->is_allowed[__NR_lstat] = 1;
p_sandbox->is_allowed[__NR_lstat64] = 1;
}
void
ptrace_sandbox_permit_fd_stats(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_fstat] = 1;
p_sandbox->is_allowed[__NR_fstat64] = 1;
}
void
ptrace_sandbox_permit_getcwd(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_getcwd] = 1;
}
void
ptrace_sandbox_permit_chdir(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_chdir] = 1;
}
void
ptrace_sandbox_permit_umask(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_umask] = 1;
}
void
ptrace_sandbox_permit_open(struct pt_sandbox* p_sandbox, int writeable)
{
p_sandbox->is_allowed[__NR_open] = 1;
if (writeable == 1)
{
p_sandbox->validator[__NR_open] = validate_open_default;
}
else
{
p_sandbox->validator[__NR_open] = validate_open_readonly;
}
}
static int
validate_open_default(struct pt_sandbox* p_sandbox, void* p_arg)
{
unsigned long arg2;
int ret = ptrace_sandbox_get_arg(p_sandbox, 1, &arg2);
(void) p_arg;
if (ret != 0)
{
return ret;
}
if (arg2 & (O_ASYNC | O_DIRECT | O_SYNC))
{
return -1;
}
return 0;
}
static int
validate_open_readonly(struct pt_sandbox* p_sandbox, void* p_arg)
{
unsigned long arg2;
int ret = validate_open_default(p_sandbox, p_arg);
if (ret != 0)
{
return ret;
}
ret = ptrace_sandbox_get_arg(p_sandbox, 1, &arg2);
if (ret != 0)
{
return ret;
}
if ((arg2 & O_ACCMODE) != O_RDONLY)
{
return -1;
}
return 0;
}
void
ptrace_sandbox_permit_close(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_close] = 1;
}
void
ptrace_sandbox_permit_getdents(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_getdents] = 1;
p_sandbox->is_allowed[__NR_getdents64] = 1;
}
void
ptrace_sandbox_permit_fcntl(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_fcntl] = 1;
p_sandbox->validator[__NR_fcntl] = validate_fcntl;
p_sandbox->is_allowed[__NR_fcntl64] = 1;
p_sandbox->validator[__NR_fcntl64] = validate_fcntl;
}
static int
validate_fcntl(struct pt_sandbox* p_sandbox, void* p_arg)
{
unsigned long arg2;
unsigned long arg3;
int ret = ptrace_sandbox_get_arg(p_sandbox, 1, &arg2);
(void) p_arg;
if (ret != 0)
{
return ret;
}
ret = ptrace_sandbox_get_arg(p_sandbox, 2, &arg3);
if (ret != 0)
{
return ret;
}
if (arg2 != F_GETFL &&
arg2 != F_SETFL &&
arg2 != F_SETOWN &&
arg2 != F_SETLK &&
arg2 != F_SETLKW &&
arg2 != F_SETLK64 &&
arg2 != F_SETLKW64 &&
arg2 != F_SETFD &&
arg2 != F_GETFD)
{
syslog(LOG_LOCAL0 | LOG_DEBUG, "fcntl not permitted: %ld", arg2);
warnx("fcntl not permitted: %ld", arg2);
return -1;
}
if (arg2 == F_SETFL && (arg3 & (O_ASYNC | O_DIRECT)))
{
return -2;
}
if (arg2 == F_SETOWN && (int) arg3 != p_sandbox->pid)
{
return -3;
}
return 0;
}
void
ptrace_sandbox_permit_sendfile(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_sendfile] = 1;
p_sandbox->is_allowed[__NR_sendfile64] = 1;
}
void
ptrace_sandbox_permit_seek(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_lseek] = 1;
p_sandbox->is_allowed[__NR__llseek] = 1;
}
void
ptrace_sandbox_permit_select(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_select] = 1;
p_sandbox->is_allowed[__NR__newselect] = 1;
}
void
ptrace_sandbox_permit_unlink(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_unlink] = 1;
}
void
ptrace_sandbox_permit_mkdir(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_mkdir] = 1;
}
void
ptrace_sandbox_permit_rmdir(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_rmdir] = 1;
}
void
ptrace_sandbox_permit_rename(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_rename] = 1;
}
void
ptrace_sandbox_permit_utime(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_utime] = 1;
p_sandbox->is_allowed[__NR_utimes] = 1;
}
void
ptrace_sandbox_permit_sigreturn(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_sigreturn] = 1;
}
void
ptrace_sandbox_permit_recv(struct pt_sandbox* p_sandbox)
{
install_socketcall(p_sandbox);
p_sandbox->is_socketcall_allowed[SYS_RECV] = 1;
}
static void
install_socketcall(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_socketcall] = 1;
p_sandbox->validator[__NR_socketcall] = validate_socketcall;
}
static int
validate_socketcall(struct pt_sandbox* p_sandbox, void* p_arg)
{
unsigned long arg1;
int ret = ptrace_sandbox_get_arg(p_sandbox, 0, &arg1);
(void) p_arg;
if (ret != 0)
{
return ret;
}
if (arg1 < 1 || arg1 >= NPROTO)
{
return -1;
}
if (p_sandbox->is_socketcall_allowed[arg1] != 1)
{
syslog(LOG_LOCAL0 | LOG_DEBUG, "socketcall not permitted: %ld", arg1);
warnx("socketcall not permitted: %ld", arg1);
return -2;
}
if (p_sandbox->socketcall_validator[arg1])
{
ptrace_sandbox_validator_t p_val = p_sandbox->socketcall_validator[arg1];
ret = (*p_val)(p_sandbox, p_sandbox->socketcall_validator_arg[arg1]);
if (ret != 0)
{
syslog(LOG_LOCAL0 | LOG_DEBUG,
"socketcall validate fail: %ld (%d)",
arg1,
ret);
warnx("socketcall validate fail: %ld (%d)", arg1, ret);
return -3;
}
}
return 0;
}
void
ptrace_sandbox_permit_readlink(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_readlink] = 1;
}
void
ptrace_sandbox_permit_brk(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_brk] = 1;
}
void
ptrace_sandbox_permit_sleep(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_nanosleep] = 1;
}
void
ptrace_sandbox_permit_fchmod(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_fchmod] = 1;
}
void
ptrace_sandbox_permit_chmod(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_chmod] = 1;
}
void
ptrace_sandbox_permit_fchown(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_fchown] = 1;
p_sandbox->is_allowed[__NR_fchown32] = 1;
}
void
ptrace_sandbox_permit_mremap(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_mremap] = 1;
}
void
ptrace_sandbox_permit_ftruncate(struct pt_sandbox* p_sandbox)
{
p_sandbox->is_allowed[__NR_ftruncate] = 1;
p_sandbox->is_allowed[__NR_ftruncate64] = 1;
}
void
ptrace_sandbox_permit_socket(struct pt_sandbox* p_sandbox)
{
install_socketcall(p_sandbox);
p_sandbox->is_socketcall_allowed[SYS_SOCKET] = 1;
}
void
ptrace_sandbox_set_socket_validator(struct pt_sandbox* p_sandbox,
ptrace_sandbox_validator_t val,
void* p_arg)
{
p_sandbox->socketcall_validator[SYS_SOCKET] = val;
p_sandbox->socketcall_validator_arg[SYS_SOCKET] = p_arg;
}
void
ptrace_sandbox_permit_bind(struct pt_sandbox* p_sandbox)
{
install_socketcall(p_sandbox);
p_sandbox->is_socketcall_allowed[SYS_BIND] = 1;
}
void
ptrace_sandbox_set_bind_validator(struct pt_sandbox* p_sandbox,
ptrace_sandbox_validator_t val,
void* p_arg)
{
p_sandbox->socketcall_validator[SYS_BIND] = val;
p_sandbox->socketcall_validator_arg[SYS_BIND] = p_arg;
}
void
ptrace_sandbox_permit_connect(struct pt_sandbox* p_sandbox)
{
install_socketcall(p_sandbox);
p_sandbox->is_socketcall_allowed[SYS_CONNECT] = 1;
}
void
ptrace_sandbox_set_connect_validator(struct pt_sandbox* p_sandbox,
ptrace_sandbox_validator_t val,
void* p_arg)
{
p_sandbox->socketcall_validator[SYS_CONNECT] = val;
p_sandbox->socketcall_validator_arg[SYS_CONNECT] = p_arg;
}
void
ptrace_sandbox_permit_listen(struct pt_sandbox* p_sandbox)
{
install_socketcall(p_sandbox);
p_sandbox->is_socketcall_allowed[SYS_LISTEN] = 1;
}
void
ptrace_sandbox_permit_accept(struct pt_sandbox* p_sandbox)
{
install_socketcall(p_sandbox);
p_sandbox->is_socketcall_allowed[SYS_ACCEPT] = 1;
}
void
ptrace_sandbox_permit_setsockopt(struct pt_sandbox* p_sandbox)
{
install_socketcall(p_sandbox);
p_sandbox->is_socketcall_allowed[SYS_SETSOCKOPT] = 1;
}
void
ptrace_sandbox_set_setsockopt_validator(struct pt_sandbox* p_sandbox,
ptrace_sandbox_validator_t val,
void* p_arg)
{
p_sandbox->socketcall_validator[SYS_SETSOCKOPT] = val;
p_sandbox->socketcall_validator_arg[SYS_SETSOCKOPT] = p_arg;
}
void
ptrace_sandbox_permit_getsockopt(struct pt_sandbox* p_sandbox)
{
install_socketcall(p_sandbox);
p_sandbox->is_socketcall_allowed[SYS_GETSOCKOPT] = 1;
}
void
ptrace_sandbox_set_getsockopt_validator(struct pt_sandbox* p_sandbox,
ptrace_sandbox_validator_t val,
void* p_arg)
{
p_sandbox->socketcall_validator[SYS_GETSOCKOPT] = val;
p_sandbox->socketcall_validator_arg[SYS_GETSOCKOPT] = p_arg;
}
void
ptrace_sandbox_permit_shutdown(struct pt_sandbox* p_sandbox)
{
install_socketcall(p_sandbox);
p_sandbox->is_socketcall_allowed[SYS_SHUTDOWN] = 1;
}
#else /* __linux__ && __i386__ */
struct pt_sandbox*
ptrace_sandbox_alloc()
{
return 0;
}
void
ptrace_sandbox_free(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
int
ptrace_sandbox_launch_process(struct pt_sandbox* p_sandbox,
void (*p_func)(void*),
void* p_arg)
{
(void) p_sandbox;
(void) p_func;
(void) p_arg;
return -1;
}
int
ptrace_sandbox_run_processes(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
return -1;
}
void
ptrace_sandbox_attach_point(void)
{
}
void
ptrace_sandbox_permit_exit(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_read(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_write(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_sigaction(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_alarm(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_query_time(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_mmap(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_mprotect(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_file_stats(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_fd_stats(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_getcwd(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_chdir(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_umask(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_open(struct pt_sandbox* p_sandbox, int writeable)
{
(void) p_sandbox;
(void) writeable;
}
void
ptrace_sandbox_permit_close(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_getdents(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_fcntl(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_sendfile(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_seek(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_select(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_unlink(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_mkdir(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_rmdir(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_rename(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_utime(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_utimes(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_sigreturn(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_recv(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_kill_processes(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
int
ptrace_sandbox_get_arg(struct pt_sandbox* p_sandbox,
int arg,
unsigned long* p_out)
{
(void) p_sandbox;
(void) arg;
(void) p_out;
return -1;
}
int
ptrace_sandbox_get_socketcall_arg(struct pt_sandbox* p_sandbox,
int arg,
unsigned long* p_out)
{
(void) p_sandbox;
(void) arg;
(void) p_out;
return -1;
}
int
ptrace_sandbox_get_long(struct pt_sandbox* p_sandbox,
unsigned long ptr,
unsigned long* p_out)
{
(void) p_sandbox;
(void) ptr;
(void) p_out;
return -1;
}
int
ptrace_sandbox_get_buf(struct pt_sandbox* p_sandbox,
unsigned long ptr,
unsigned long len,
void* p_buf)
{
(void) p_sandbox;
(void) ptr;
(void) len;
(void) p_buf;
return -1;
}
void
ptrace_sandbox_permit_readlink(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_brk(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_sleep(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_fchmod(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_chmod(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_fchown(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_mremap(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_ftruncate(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_socket(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_set_socket_validator(struct pt_sandbox* p_sandbox,
ptrace_sandbox_validator_t val,
void* p_arg)
{
(void) p_sandbox;
(void) val;
(void) p_arg;
}
void
ptrace_sandbox_permit_bind(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_set_bind_validator(struct pt_sandbox* p_sandbox,
ptrace_sandbox_validator_t val,
void* p_arg)
{
(void) p_sandbox;
(void) val;
(void) p_arg;
}
void
ptrace_sandbox_permit_connect(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_set_connect_validator(struct pt_sandbox* p_sandbox,
ptrace_sandbox_validator_t val,
void* p_arg)
{
(void) p_sandbox;
(void) val;
(void) p_arg;
}
void
ptrace_sandbox_permit_listen(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_accept(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_permit_setsockopt(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_set_setsockopt_validator(struct pt_sandbox* p_sandbox,
ptrace_sandbox_validator_t val,
void* p_arg)
{
(void) p_sandbox;
(void) val;
(void) p_arg;
}
void
ptrace_sandbox_permit_getsockopt(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
void
ptrace_sandbox_set_getsockopt_validator(struct pt_sandbox* p_sandbox,
ptrace_sandbox_validator_t val,
void* p_arg)
{
(void) p_sandbox;
(void) val;
(void) p_arg;
}
void
ptrace_sandbox_permit_shutdown(struct pt_sandbox* p_sandbox)
{
(void) p_sandbox;
}
#endif /* __linux__ && __i386__ */
|