diff options
Diffstat (limited to 'kernel/sys.c')
| -rw-r--r-- | kernel/sys.c | 290 |
1 files changed, 7 insertions, 283 deletions
diff --git a/kernel/sys.c b/kernel/sys.c index e7998cf31498..b3f1097c76fa 100644 --- a/kernel/sys.c +++ b/kernel/sys.c | |||
| @@ -14,6 +14,7 @@ | |||
| 14 | #include <linux/prctl.h> | 14 | #include <linux/prctl.h> |
| 15 | #include <linux/highuid.h> | 15 | #include <linux/highuid.h> |
| 16 | #include <linux/fs.h> | 16 | #include <linux/fs.h> |
| 17 | #include <linux/perf_counter.h> | ||
| 17 | #include <linux/resource.h> | 18 | #include <linux/resource.h> |
| 18 | #include <linux/kernel.h> | 19 | #include <linux/kernel.h> |
| 19 | #include <linux/kexec.h> | 20 | #include <linux/kexec.h> |
| @@ -1112,289 +1113,6 @@ out: | |||
| 1112 | return err; | 1113 | return err; |
| 1113 | } | 1114 | } |
| 1114 | 1115 | ||
| 1115 | /* | ||
| 1116 | * Supplementary group IDs | ||
| 1117 | */ | ||
| 1118 | |||
| 1119 | /* init to 2 - one for init_task, one to ensure it is never freed */ | ||
| 1120 | struct group_info init_groups = { .usage = ATOMIC_INIT(2) }; | ||
| 1121 | |||
| 1122 | struct group_info *groups_alloc(int gidsetsize) | ||
| 1123 | { | ||
| 1124 | struct group_info *group_info; | ||
| 1125 | int nblocks; | ||
| 1126 | int i; | ||
| 1127 | |||
| 1128 | nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK; | ||
| 1129 | /* Make sure we always allocate at least one indirect block pointer */ | ||
| 1130 | nblocks = nblocks ? : 1; | ||
| 1131 | group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER); | ||
| 1132 | if (!group_info) | ||
| 1133 | return NULL; | ||
| 1134 | group_info->ngroups = gidsetsize; | ||
| 1135 | group_info->nblocks = nblocks; | ||
| 1136 | atomic_set(&group_info->usage, 1); | ||
| 1137 | |||
| 1138 | if (gidsetsize <= NGROUPS_SMALL) | ||
| 1139 | group_info->blocks[0] = group_info->small_block; | ||
| 1140 | else { | ||
| 1141 | for (i = 0; i < nblocks; i++) { | ||
| 1142 | gid_t *b; | ||
| 1143 | b = (void *)__get_free_page(GFP_USER); | ||
| 1144 | if (!b) | ||
| 1145 | goto out_undo_partial_alloc; | ||
| 1146 | group_info->blocks[i] = b; | ||
| 1147 | } | ||
| 1148 | } | ||
| 1149 | return group_info; | ||
| 1150 | |||
| 1151 | out_undo_partial_alloc: | ||
| 1152 | while (--i >= 0) { | ||
| 1153 | free_page((unsigned long)group_info->blocks[i]); | ||
| 1154 | } | ||
| 1155 | kfree(group_info); | ||
| 1156 | return NULL; | ||
| 1157 | } | ||
| 1158 | |||
| 1159 | EXPORT_SYMBOL(groups_alloc); | ||
| 1160 | |||
| 1161 | void groups_free(struct group_info *group_info) | ||
| 1162 | { | ||
| 1163 | if (group_info->blocks[0] != group_info->small_block) { | ||
| 1164 | int i; | ||
| 1165 | for (i = 0; i < group_info->nblocks; i++) | ||
| 1166 | free_page((unsigned long)group_info->blocks[i]); | ||
| 1167 | } | ||
| 1168 | kfree(group_info); | ||
| 1169 | } | ||
| 1170 | |||
| 1171 | EXPORT_SYMBOL(groups_free); | ||
| 1172 | |||
| 1173 | /* export the group_info to a user-space array */ | ||
| 1174 | static int groups_to_user(gid_t __user *grouplist, | ||
| 1175 | const struct group_info *group_info) | ||
| 1176 | { | ||
| 1177 | int i; | ||
| 1178 | unsigned int count = group_info->ngroups; | ||
| 1179 | |||
| 1180 | for (i = 0; i < group_info->nblocks; i++) { | ||
| 1181 | unsigned int cp_count = min(NGROUPS_PER_BLOCK, count); | ||
| 1182 | unsigned int len = cp_count * sizeof(*grouplist); | ||
| 1183 | |||
| 1184 | if (copy_to_user(grouplist, group_info->blocks[i], len)) | ||
| 1185 | return -EFAULT; | ||
| 1186 | |||
| 1187 | grouplist += NGROUPS_PER_BLOCK; | ||
| 1188 | count -= cp_count; | ||
| 1189 | } | ||
| 1190 | return 0; | ||
| 1191 | } | ||
| 1192 | |||
| 1193 | /* fill a group_info from a user-space array - it must be allocated already */ | ||
| 1194 | static int groups_from_user(struct group_info *group_info, | ||
| 1195 | gid_t __user *grouplist) | ||
| 1196 | { | ||
| 1197 | int i; | ||
| 1198 | unsigned int count = group_info->ngroups; | ||
| 1199 | |||
| 1200 | for (i = 0; i < group_info->nblocks; i++) { | ||
| 1201 | unsigned int cp_count = min(NGROUPS_PER_BLOCK, count); | ||
| 1202 | unsigned int len = cp_count * sizeof(*grouplist); | ||
| 1203 | |||
| 1204 | if (copy_from_user(group_info->blocks[i], grouplist, len)) | ||
| 1205 | return -EFAULT; | ||
| 1206 | |||
| 1207 | grouplist += NGROUPS_PER_BLOCK; | ||
| 1208 | count -= cp_count; | ||
| 1209 | } | ||
| 1210 | return 0; | ||
| 1211 | } | ||
| 1212 | |||
| 1213 | /* a simple Shell sort */ | ||
| 1214 | static void groups_sort(struct group_info *group_info) | ||
| 1215 | { | ||
| 1216 | int base, max, stride; | ||
| 1217 | int gidsetsize = group_info->ngroups; | ||
| 1218 | |||
| 1219 | for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1) | ||
| 1220 | ; /* nothing */ | ||
| 1221 | stride /= 3; | ||
| 1222 | |||
| 1223 | while (stride) { | ||
| 1224 | max = gidsetsize - stride; | ||
| 1225 | for (base = 0; base < max; base++) { | ||
| 1226 | int left = base; | ||
| 1227 | int right = left + stride; | ||
| 1228 | gid_t tmp = GROUP_AT(group_info, right); | ||
| 1229 | |||
| 1230 | while (left >= 0 && GROUP_AT(group_info, left) > tmp) { | ||
| 1231 | GROUP_AT(group_info, right) = | ||
| 1232 | GROUP_AT(group_info, left); | ||
| 1233 | right = left; | ||
| 1234 | left -= stride; | ||
| 1235 | } | ||
| 1236 | GROUP_AT(group_info, right) = tmp; | ||
| 1237 | } | ||
| 1238 | stride /= 3; | ||
| 1239 | } | ||
| 1240 | } | ||
| 1241 | |||
| 1242 | /* a simple bsearch */ | ||
| 1243 | int groups_search(const struct group_info *group_info, gid_t grp) | ||
| 1244 | { | ||
| 1245 | unsigned int left, right; | ||
| 1246 | |||
| 1247 | if (!group_info) | ||
| 1248 | return 0; | ||
| 1249 | |||
| 1250 | left = 0; | ||
| 1251 | right = group_info->ngroups; | ||
| 1252 | while (left < right) { | ||
| 1253 | unsigned int mid = (left+right)/2; | ||
| 1254 | int cmp = grp - GROUP_AT(group_info, mid); | ||
| 1255 | if (cmp > 0) | ||
| 1256 | left = mid + 1; | ||
| 1257 | else if (cmp < 0) | ||
| 1258 | right = mid; | ||
| 1259 | else | ||
| 1260 | return 1; | ||
| 1261 | } | ||
| 1262 | return 0; | ||
| 1263 | } | ||
| 1264 | |||
| 1265 | /** | ||
| 1266 | * set_groups - Change a group subscription in a set of credentials | ||
| 1267 | * @new: The newly prepared set of credentials to alter | ||
| 1268 | * @group_info: The group list to install | ||
| 1269 | * | ||
| 1270 | * Validate a group subscription and, if valid, insert it into a set | ||
| 1271 | * of credentials. | ||
| 1272 | */ | ||
| 1273 | int set_groups(struct cred *new, struct group_info *group_info) | ||
| 1274 | { | ||
| 1275 | int retval; | ||
| 1276 | |||
| 1277 | retval = security_task_setgroups(group_info); | ||
| 1278 | if (retval) | ||
| 1279 | return retval; | ||
| 1280 | |||
| 1281 | put_group_info(new->group_info); | ||
| 1282 | groups_sort(group_info); | ||
| 1283 | get_group_info(group_info); | ||
| 1284 | new->group_info = group_info; | ||
| 1285 | return 0; | ||
| 1286 | } | ||
| 1287 | |||
| 1288 | EXPORT_SYMBOL(set_groups); | ||
| 1289 | |||
| 1290 | /** | ||
| 1291 | * set_current_groups - Change current's group subscription | ||
| 1292 | * @group_info: The group list to impose | ||
| 1293 | * | ||
| 1294 | * Validate a group subscription and, if valid, impose it upon current's task | ||
| 1295 | * security record. | ||
| 1296 | */ | ||
| 1297 | int set_current_groups(struct group_info *group_info) | ||
| 1298 | { | ||
| 1299 | struct cred *new; | ||
| 1300 | int ret; | ||
| 1301 | |||
| 1302 | new = prepare_creds(); | ||
| 1303 | if (!new) | ||
| 1304 | return -ENOMEM; | ||
| 1305 | |||
| 1306 | ret = set_groups(new, group_info); | ||
| 1307 | if (ret < 0) { | ||
| 1308 | abort_creds(new); | ||
| 1309 | return ret; | ||
| 1310 | } | ||
| 1311 | |||
| 1312 | return commit_creds(new); | ||
| 1313 | } | ||
| 1314 | |||
| 1315 | EXPORT_SYMBOL(set_current_groups); | ||
| 1316 | |||
| 1317 | SYSCALL_DEFINE2(getgroups, int, gidsetsize, gid_t __user *, grouplist) | ||
| 1318 | { | ||
| 1319 | const struct cred *cred = current_cred(); | ||
| 1320 | int i; | ||
| 1321 | |||
| 1322 | if (gidsetsize < 0) | ||
| 1323 | return -EINVAL; | ||
| 1324 | |||
| 1325 | /* no need to grab task_lock here; it cannot change */ | ||
| 1326 | i = cred->group_info->ngroups; | ||
| 1327 | if (gidsetsize) { | ||
| 1328 | if (i > gidsetsize) { | ||
| 1329 | i = -EINVAL; | ||
| 1330 | goto out; | ||
| 1331 | } | ||
| 1332 | if (groups_to_user(grouplist, cred->group_info)) { | ||
| 1333 | i = -EFAULT; | ||
| 1334 | goto out; | ||
| 1335 | } | ||
| 1336 | } | ||
| 1337 | out: | ||
| 1338 | return i; | ||
| 1339 | } | ||
| 1340 | |||
| 1341 | /* | ||
| 1342 | * SMP: Our groups are copy-on-write. We can set them safely | ||
| 1343 | * without another task interfering. | ||
| 1344 | */ | ||
| 1345 | |||
| 1346 | SYSCALL_DEFINE2(setgroups, int, gidsetsize, gid_t __user *, grouplist) | ||
| 1347 | { | ||
| 1348 | struct group_info *group_info; | ||
| 1349 | int retval; | ||
| 1350 | |||
| 1351 | if (!capable(CAP_SETGID)) | ||
| 1352 | return -EPERM; | ||
| 1353 | if ((unsigned)gidsetsize > NGROUPS_MAX) | ||
| 1354 | return -EINVAL; | ||
| 1355 | |||
| 1356 | group_info = groups_alloc(gidsetsize); | ||
| 1357 | if (!group_info) | ||
| 1358 | return -ENOMEM; | ||
| 1359 | retval = groups_from_user(group_info, grouplist); | ||
| 1360 | if (retval) { | ||
| 1361 | put_group_info(group_info); | ||
| 1362 | return retval; | ||
| 1363 | } | ||
| 1364 | |||
| 1365 | retval = set_current_groups(group_info); | ||
| 1366 | put_group_info(group_info); | ||
| 1367 | |||
| 1368 | return retval; | ||
| 1369 | } | ||
| 1370 | |||
| 1371 | /* | ||
| 1372 | * Check whether we're fsgid/egid or in the supplemental group.. | ||
| 1373 | */ | ||
| 1374 | int in_group_p(gid_t grp) | ||
| 1375 | { | ||
| 1376 | const struct cred *cred = current_cred(); | ||
| 1377 | int retval = 1; | ||
| 1378 | |||
| 1379 | if (grp != cred->fsgid) | ||
| 1380 | retval = groups_search(cred->group_info, grp); | ||
| 1381 | return retval; | ||
| 1382 | } | ||
| 1383 | |||
| 1384 | EXPORT_SYMBOL(in_group_p); | ||
| 1385 | |||
| 1386 | int in_egroup_p(gid_t grp) | ||
| 1387 | { | ||
| 1388 | const struct cred *cred = current_cred(); | ||
| 1389 | int retval = 1; | ||
| 1390 | |||
| 1391 | if (grp != cred->egid) | ||
| 1392 | retval = groups_search(cred->group_info, grp); | ||
| 1393 | return retval; | ||
| 1394 | } | ||
| 1395 | |||
| 1396 | EXPORT_SYMBOL(in_egroup_p); | ||
| 1397 | |||
| 1398 | DECLARE_RWSEM(uts_sem); | 1116 | DECLARE_RWSEM(uts_sem); |
| 1399 | 1117 | ||
| 1400 | SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name) | 1118 | SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name) |
| @@ -1793,6 +1511,12 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, | |||
| 1793 | case PR_SET_TSC: | 1511 | case PR_SET_TSC: |
| 1794 | error = SET_TSC_CTL(arg2); | 1512 | error = SET_TSC_CTL(arg2); |
| 1795 | break; | 1513 | break; |
| 1514 | case PR_TASK_PERF_COUNTERS_DISABLE: | ||
| 1515 | error = perf_counter_task_disable(); | ||
| 1516 | break; | ||
| 1517 | case PR_TASK_PERF_COUNTERS_ENABLE: | ||
| 1518 | error = perf_counter_task_enable(); | ||
| 1519 | break; | ||
| 1796 | case PR_GET_TIMERSLACK: | 1520 | case PR_GET_TIMERSLACK: |
| 1797 | error = current->timer_slack_ns; | 1521 | error = current->timer_slack_ns; |
| 1798 | break; | 1522 | break; |
