diff options
Diffstat (limited to 'fs/exec.c')
-rw-r--r-- | fs/exec.c | 183 |
1 files changed, 109 insertions, 74 deletions
@@ -55,6 +55,7 @@ | |||
55 | #include <asm/uaccess.h> | 55 | #include <asm/uaccess.h> |
56 | #include <asm/mmu_context.h> | 56 | #include <asm/mmu_context.h> |
57 | #include <asm/tlb.h> | 57 | #include <asm/tlb.h> |
58 | #include "internal.h" | ||
58 | 59 | ||
59 | #ifdef __alpha__ | 60 | #ifdef __alpha__ |
60 | /* for /sbin/loader handling in search_binary_handler() */ | 61 | /* for /sbin/loader handling in search_binary_handler() */ |
@@ -978,7 +979,7 @@ int flush_old_exec(struct linux_binprm * bprm) | |||
978 | /* This is the point of no return */ | 979 | /* This is the point of no return */ |
979 | current->sas_ss_sp = current->sas_ss_size = 0; | 980 | current->sas_ss_sp = current->sas_ss_size = 0; |
980 | 981 | ||
981 | if (current->euid == current->uid && current->egid == current->gid) | 982 | if (current_euid() == current_uid() && current_egid() == current_gid()) |
982 | set_dumpable(current->mm, 1); | 983 | set_dumpable(current->mm, 1); |
983 | else | 984 | else |
984 | set_dumpable(current->mm, suid_dumpable); | 985 | set_dumpable(current->mm, suid_dumpable); |
@@ -1005,16 +1006,17 @@ int flush_old_exec(struct linux_binprm * bprm) | |||
1005 | */ | 1006 | */ |
1006 | current->mm->task_size = TASK_SIZE; | 1007 | current->mm->task_size = TASK_SIZE; |
1007 | 1008 | ||
1008 | if (bprm->e_uid != current->euid || bprm->e_gid != current->egid) { | 1009 | /* install the new credentials */ |
1009 | suid_keys(current); | 1010 | if (bprm->cred->uid != current_euid() || |
1010 | set_dumpable(current->mm, suid_dumpable); | 1011 | bprm->cred->gid != current_egid()) { |
1011 | current->pdeath_signal = 0; | 1012 | current->pdeath_signal = 0; |
1012 | } else if (file_permission(bprm->file, MAY_READ) || | 1013 | } else if (file_permission(bprm->file, MAY_READ) || |
1013 | (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) { | 1014 | bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP) { |
1014 | suid_keys(current); | ||
1015 | set_dumpable(current->mm, suid_dumpable); | 1015 | set_dumpable(current->mm, suid_dumpable); |
1016 | } | 1016 | } |
1017 | 1017 | ||
1018 | current->personality &= ~bprm->per_clear; | ||
1019 | |||
1018 | /* An exec changes our domain. We are no longer part of the thread | 1020 | /* An exec changes our domain. We are no longer part of the thread |
1019 | group */ | 1021 | group */ |
1020 | 1022 | ||
@@ -1031,13 +1033,50 @@ out: | |||
1031 | 1033 | ||
1032 | EXPORT_SYMBOL(flush_old_exec); | 1034 | EXPORT_SYMBOL(flush_old_exec); |
1033 | 1035 | ||
1036 | /* | ||
1037 | * install the new credentials for this executable | ||
1038 | */ | ||
1039 | void install_exec_creds(struct linux_binprm *bprm) | ||
1040 | { | ||
1041 | security_bprm_committing_creds(bprm); | ||
1042 | |||
1043 | commit_creds(bprm->cred); | ||
1044 | bprm->cred = NULL; | ||
1045 | |||
1046 | /* cred_exec_mutex must be held at least to this point to prevent | ||
1047 | * ptrace_attach() from altering our determination of the task's | ||
1048 | * credentials; any time after this it may be unlocked */ | ||
1049 | |||
1050 | security_bprm_committed_creds(bprm); | ||
1051 | } | ||
1052 | EXPORT_SYMBOL(install_exec_creds); | ||
1053 | |||
1054 | /* | ||
1055 | * determine how safe it is to execute the proposed program | ||
1056 | * - the caller must hold current->cred_exec_mutex to protect against | ||
1057 | * PTRACE_ATTACH | ||
1058 | */ | ||
1059 | void check_unsafe_exec(struct linux_binprm *bprm) | ||
1060 | { | ||
1061 | struct task_struct *p = current; | ||
1062 | |||
1063 | bprm->unsafe = tracehook_unsafe_exec(p); | ||
1064 | |||
1065 | if (atomic_read(&p->fs->count) > 1 || | ||
1066 | atomic_read(&p->files->count) > 1 || | ||
1067 | atomic_read(&p->sighand->count) > 1) | ||
1068 | bprm->unsafe |= LSM_UNSAFE_SHARE; | ||
1069 | } | ||
1070 | |||
1034 | /* | 1071 | /* |
1035 | * Fill the binprm structure from the inode. | 1072 | * Fill the binprm structure from the inode. |
1036 | * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes | 1073 | * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes |
1074 | * | ||
1075 | * This may be called multiple times for binary chains (scripts for example). | ||
1037 | */ | 1076 | */ |
1038 | int prepare_binprm(struct linux_binprm *bprm) | 1077 | int prepare_binprm(struct linux_binprm *bprm) |
1039 | { | 1078 | { |
1040 | int mode; | 1079 | umode_t mode; |
1041 | struct inode * inode = bprm->file->f_path.dentry->d_inode; | 1080 | struct inode * inode = bprm->file->f_path.dentry->d_inode; |
1042 | int retval; | 1081 | int retval; |
1043 | 1082 | ||
@@ -1045,14 +1084,15 @@ int prepare_binprm(struct linux_binprm *bprm) | |||
1045 | if (bprm->file->f_op == NULL) | 1084 | if (bprm->file->f_op == NULL) |
1046 | return -EACCES; | 1085 | return -EACCES; |
1047 | 1086 | ||
1048 | bprm->e_uid = current->euid; | 1087 | /* clear any previous set[ug]id data from a previous binary */ |
1049 | bprm->e_gid = current->egid; | 1088 | bprm->cred->euid = current_euid(); |
1089 | bprm->cred->egid = current_egid(); | ||
1050 | 1090 | ||
1051 | if(!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) { | 1091 | if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) { |
1052 | /* Set-uid? */ | 1092 | /* Set-uid? */ |
1053 | if (mode & S_ISUID) { | 1093 | if (mode & S_ISUID) { |
1054 | current->personality &= ~PER_CLEAR_ON_SETID; | 1094 | bprm->per_clear |= PER_CLEAR_ON_SETID; |
1055 | bprm->e_uid = inode->i_uid; | 1095 | bprm->cred->euid = inode->i_uid; |
1056 | } | 1096 | } |
1057 | 1097 | ||
1058 | /* Set-gid? */ | 1098 | /* Set-gid? */ |
@@ -1062,52 +1102,23 @@ int prepare_binprm(struct linux_binprm *bprm) | |||
1062 | * executable. | 1102 | * executable. |
1063 | */ | 1103 | */ |
1064 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { | 1104 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { |
1065 | current->personality &= ~PER_CLEAR_ON_SETID; | 1105 | bprm->per_clear |= PER_CLEAR_ON_SETID; |
1066 | bprm->e_gid = inode->i_gid; | 1106 | bprm->cred->egid = inode->i_gid; |
1067 | } | 1107 | } |
1068 | } | 1108 | } |
1069 | 1109 | ||
1070 | /* fill in binprm security blob */ | 1110 | /* fill in binprm security blob */ |
1071 | retval = security_bprm_set(bprm); | 1111 | retval = security_bprm_set_creds(bprm); |
1072 | if (retval) | 1112 | if (retval) |
1073 | return retval; | 1113 | return retval; |
1114 | bprm->cred_prepared = 1; | ||
1074 | 1115 | ||
1075 | memset(bprm->buf,0,BINPRM_BUF_SIZE); | 1116 | memset(bprm->buf, 0, BINPRM_BUF_SIZE); |
1076 | return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE); | 1117 | return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE); |
1077 | } | 1118 | } |
1078 | 1119 | ||
1079 | EXPORT_SYMBOL(prepare_binprm); | 1120 | EXPORT_SYMBOL(prepare_binprm); |
1080 | 1121 | ||
1081 | static int unsafe_exec(struct task_struct *p) | ||
1082 | { | ||
1083 | int unsafe = tracehook_unsafe_exec(p); | ||
1084 | |||
1085 | if (atomic_read(&p->fs->count) > 1 || | ||
1086 | atomic_read(&p->files->count) > 1 || | ||
1087 | atomic_read(&p->sighand->count) > 1) | ||
1088 | unsafe |= LSM_UNSAFE_SHARE; | ||
1089 | |||
1090 | return unsafe; | ||
1091 | } | ||
1092 | |||
1093 | void compute_creds(struct linux_binprm *bprm) | ||
1094 | { | ||
1095 | int unsafe; | ||
1096 | |||
1097 | if (bprm->e_uid != current->uid) { | ||
1098 | suid_keys(current); | ||
1099 | current->pdeath_signal = 0; | ||
1100 | } | ||
1101 | exec_keys(current); | ||
1102 | |||
1103 | task_lock(current); | ||
1104 | unsafe = unsafe_exec(current); | ||
1105 | security_bprm_apply_creds(bprm, unsafe); | ||
1106 | task_unlock(current); | ||
1107 | security_bprm_post_apply_creds(bprm); | ||
1108 | } | ||
1109 | EXPORT_SYMBOL(compute_creds); | ||
1110 | |||
1111 | /* | 1122 | /* |
1112 | * Arguments are '\0' separated strings found at the location bprm->p | 1123 | * Arguments are '\0' separated strings found at the location bprm->p |
1113 | * points to; chop off the first by relocating brpm->p to right after | 1124 | * points to; chop off the first by relocating brpm->p to right after |
@@ -1268,6 +1279,8 @@ EXPORT_SYMBOL(search_binary_handler); | |||
1268 | void free_bprm(struct linux_binprm *bprm) | 1279 | void free_bprm(struct linux_binprm *bprm) |
1269 | { | 1280 | { |
1270 | free_arg_pages(bprm); | 1281 | free_arg_pages(bprm); |
1282 | if (bprm->cred) | ||
1283 | abort_creds(bprm->cred); | ||
1271 | kfree(bprm); | 1284 | kfree(bprm); |
1272 | } | 1285 | } |
1273 | 1286 | ||
@@ -1293,10 +1306,20 @@ int do_execve(char * filename, | |||
1293 | if (!bprm) | 1306 | if (!bprm) |
1294 | goto out_files; | 1307 | goto out_files; |
1295 | 1308 | ||
1309 | retval = mutex_lock_interruptible(¤t->cred_exec_mutex); | ||
1310 | if (retval < 0) | ||
1311 | goto out_free; | ||
1312 | |||
1313 | retval = -ENOMEM; | ||
1314 | bprm->cred = prepare_exec_creds(); | ||
1315 | if (!bprm->cred) | ||
1316 | goto out_unlock; | ||
1317 | check_unsafe_exec(bprm); | ||
1318 | |||
1296 | file = open_exec(filename); | 1319 | file = open_exec(filename); |
1297 | retval = PTR_ERR(file); | 1320 | retval = PTR_ERR(file); |
1298 | if (IS_ERR(file)) | 1321 | if (IS_ERR(file)) |
1299 | goto out_kfree; | 1322 | goto out_unlock; |
1300 | 1323 | ||
1301 | sched_exec(); | 1324 | sched_exec(); |
1302 | 1325 | ||
@@ -1310,14 +1333,10 @@ int do_execve(char * filename, | |||
1310 | 1333 | ||
1311 | bprm->argc = count(argv, MAX_ARG_STRINGS); | 1334 | bprm->argc = count(argv, MAX_ARG_STRINGS); |
1312 | if ((retval = bprm->argc) < 0) | 1335 | if ((retval = bprm->argc) < 0) |
1313 | goto out_mm; | 1336 | goto out; |
1314 | 1337 | ||
1315 | bprm->envc = count(envp, MAX_ARG_STRINGS); | 1338 | bprm->envc = count(envp, MAX_ARG_STRINGS); |
1316 | if ((retval = bprm->envc) < 0) | 1339 | if ((retval = bprm->envc) < 0) |
1317 | goto out_mm; | ||
1318 | |||
1319 | retval = security_bprm_alloc(bprm); | ||
1320 | if (retval) | ||
1321 | goto out; | 1340 | goto out; |
1322 | 1341 | ||
1323 | retval = prepare_binprm(bprm); | 1342 | retval = prepare_binprm(bprm); |
@@ -1339,21 +1358,18 @@ int do_execve(char * filename, | |||
1339 | 1358 | ||
1340 | current->flags &= ~PF_KTHREAD; | 1359 | current->flags &= ~PF_KTHREAD; |
1341 | retval = search_binary_handler(bprm,regs); | 1360 | retval = search_binary_handler(bprm,regs); |
1342 | if (retval >= 0) { | 1361 | if (retval < 0) |
1343 | /* execve success */ | 1362 | goto out; |
1344 | security_bprm_free(bprm); | ||
1345 | acct_update_integrals(current); | ||
1346 | free_bprm(bprm); | ||
1347 | if (displaced) | ||
1348 | put_files_struct(displaced); | ||
1349 | return retval; | ||
1350 | } | ||
1351 | 1363 | ||
1352 | out: | 1364 | /* execve succeeded */ |
1353 | if (bprm->security) | 1365 | mutex_unlock(¤t->cred_exec_mutex); |
1354 | security_bprm_free(bprm); | 1366 | acct_update_integrals(current); |
1367 | free_bprm(bprm); | ||
1368 | if (displaced) | ||
1369 | put_files_struct(displaced); | ||
1370 | return retval; | ||
1355 | 1371 | ||
1356 | out_mm: | 1372 | out: |
1357 | if (bprm->mm) | 1373 | if (bprm->mm) |
1358 | mmput (bprm->mm); | 1374 | mmput (bprm->mm); |
1359 | 1375 | ||
@@ -1362,7 +1378,11 @@ out_file: | |||
1362 | allow_write_access(bprm->file); | 1378 | allow_write_access(bprm->file); |
1363 | fput(bprm->file); | 1379 | fput(bprm->file); |
1364 | } | 1380 | } |
1365 | out_kfree: | 1381 | |
1382 | out_unlock: | ||
1383 | mutex_unlock(¤t->cred_exec_mutex); | ||
1384 | |||
1385 | out_free: | ||
1366 | free_bprm(bprm); | 1386 | free_bprm(bprm); |
1367 | 1387 | ||
1368 | out_files: | 1388 | out_files: |
@@ -1394,6 +1414,7 @@ EXPORT_SYMBOL(set_binfmt); | |||
1394 | */ | 1414 | */ |
1395 | static int format_corename(char *corename, long signr) | 1415 | static int format_corename(char *corename, long signr) |
1396 | { | 1416 | { |
1417 | const struct cred *cred = current_cred(); | ||
1397 | const char *pat_ptr = core_pattern; | 1418 | const char *pat_ptr = core_pattern; |
1398 | int ispipe = (*pat_ptr == '|'); | 1419 | int ispipe = (*pat_ptr == '|'); |
1399 | char *out_ptr = corename; | 1420 | char *out_ptr = corename; |
@@ -1430,7 +1451,7 @@ static int format_corename(char *corename, long signr) | |||
1430 | /* uid */ | 1451 | /* uid */ |
1431 | case 'u': | 1452 | case 'u': |
1432 | rc = snprintf(out_ptr, out_end - out_ptr, | 1453 | rc = snprintf(out_ptr, out_end - out_ptr, |
1433 | "%d", current->uid); | 1454 | "%d", cred->uid); |
1434 | if (rc > out_end - out_ptr) | 1455 | if (rc > out_end - out_ptr) |
1435 | goto out; | 1456 | goto out; |
1436 | out_ptr += rc; | 1457 | out_ptr += rc; |
@@ -1438,7 +1459,7 @@ static int format_corename(char *corename, long signr) | |||
1438 | /* gid */ | 1459 | /* gid */ |
1439 | case 'g': | 1460 | case 'g': |
1440 | rc = snprintf(out_ptr, out_end - out_ptr, | 1461 | rc = snprintf(out_ptr, out_end - out_ptr, |
1441 | "%d", current->gid); | 1462 | "%d", cred->gid); |
1442 | if (rc > out_end - out_ptr) | 1463 | if (rc > out_end - out_ptr) |
1443 | goto out; | 1464 | goto out; |
1444 | out_ptr += rc; | 1465 | out_ptr += rc; |
@@ -1714,8 +1735,9 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs) | |||
1714 | struct linux_binfmt * binfmt; | 1735 | struct linux_binfmt * binfmt; |
1715 | struct inode * inode; | 1736 | struct inode * inode; |
1716 | struct file * file; | 1737 | struct file * file; |
1738 | const struct cred *old_cred; | ||
1739 | struct cred *cred; | ||
1717 | int retval = 0; | 1740 | int retval = 0; |
1718 | int fsuid = current->fsuid; | ||
1719 | int flag = 0; | 1741 | int flag = 0; |
1720 | int ispipe = 0; | 1742 | int ispipe = 0; |
1721 | unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur; | 1743 | unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur; |
@@ -1728,12 +1750,20 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs) | |||
1728 | binfmt = current->binfmt; | 1750 | binfmt = current->binfmt; |
1729 | if (!binfmt || !binfmt->core_dump) | 1751 | if (!binfmt || !binfmt->core_dump) |
1730 | goto fail; | 1752 | goto fail; |
1753 | |||
1754 | cred = prepare_creds(); | ||
1755 | if (!cred) { | ||
1756 | retval = -ENOMEM; | ||
1757 | goto fail; | ||
1758 | } | ||
1759 | |||
1731 | down_write(&mm->mmap_sem); | 1760 | down_write(&mm->mmap_sem); |
1732 | /* | 1761 | /* |
1733 | * If another thread got here first, or we are not dumpable, bail out. | 1762 | * If another thread got here first, or we are not dumpable, bail out. |
1734 | */ | 1763 | */ |
1735 | if (mm->core_state || !get_dumpable(mm)) { | 1764 | if (mm->core_state || !get_dumpable(mm)) { |
1736 | up_write(&mm->mmap_sem); | 1765 | up_write(&mm->mmap_sem); |
1766 | put_cred(cred); | ||
1737 | goto fail; | 1767 | goto fail; |
1738 | } | 1768 | } |
1739 | 1769 | ||
@@ -1744,12 +1774,16 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs) | |||
1744 | */ | 1774 | */ |
1745 | if (get_dumpable(mm) == 2) { /* Setuid core dump mode */ | 1775 | if (get_dumpable(mm) == 2) { /* Setuid core dump mode */ |
1746 | flag = O_EXCL; /* Stop rewrite attacks */ | 1776 | flag = O_EXCL; /* Stop rewrite attacks */ |
1747 | current->fsuid = 0; /* Dump root private */ | 1777 | cred->fsuid = 0; /* Dump root private */ |
1748 | } | 1778 | } |
1749 | 1779 | ||
1750 | retval = coredump_wait(exit_code, &core_state); | 1780 | retval = coredump_wait(exit_code, &core_state); |
1751 | if (retval < 0) | 1781 | if (retval < 0) { |
1782 | put_cred(cred); | ||
1752 | goto fail; | 1783 | goto fail; |
1784 | } | ||
1785 | |||
1786 | old_cred = override_creds(cred); | ||
1753 | 1787 | ||
1754 | /* | 1788 | /* |
1755 | * Clear any false indication of pending signals that might | 1789 | * Clear any false indication of pending signals that might |
@@ -1821,7 +1855,7 @@ int do_coredump(long signr, int exit_code, struct pt_regs * regs) | |||
1821 | * Dont allow local users get cute and trick others to coredump | 1855 | * Dont allow local users get cute and trick others to coredump |
1822 | * into their pre-created files: | 1856 | * into their pre-created files: |
1823 | */ | 1857 | */ |
1824 | if (inode->i_uid != current->fsuid) | 1858 | if (inode->i_uid != current_fsuid()) |
1825 | goto close_fail; | 1859 | goto close_fail; |
1826 | if (!file->f_op) | 1860 | if (!file->f_op) |
1827 | goto close_fail; | 1861 | goto close_fail; |
@@ -1840,7 +1874,8 @@ fail_unlock: | |||
1840 | if (helper_argv) | 1874 | if (helper_argv) |
1841 | argv_free(helper_argv); | 1875 | argv_free(helper_argv); |
1842 | 1876 | ||
1843 | current->fsuid = fsuid; | 1877 | revert_creds(old_cred); |
1878 | put_cred(cred); | ||
1844 | coredump_finish(mm); | 1879 | coredump_finish(mm); |
1845 | fail: | 1880 | fail: |
1846 | return retval; | 1881 | return retval; |