Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

prlimit: make do_prlimit() static

There are no other callers in the kernel.

Fixed up a comment format and whitespace issue when moving do_prlimit()
higher in sys.c.

Signed-off-by: Barret Rhoden <brho@google.com>
Link: https://lkml.kernel.org/r/20220106172041.522167-3-brho@google.com
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>

authored by

Barret Rhoden and committed by
Eric W. Biederman
c57bef02 ffb217a1

+59 -59
-2
include/linux/resource.h
··· 8 8 struct task_struct; 9 9 10 10 void getrusage(struct task_struct *p, int who, struct rusage *ru); 11 - int do_prlimit(struct task_struct *tsk, unsigned int resource, 12 - struct rlimit *new_rlim, struct rlimit *old_rlim); 13 11 14 12 #endif
+59 -57
kernel/sys.c
··· 1424 1424 return errno; 1425 1425 } 1426 1426 1427 + /* make sure you are allowed to change @tsk limits before calling this */ 1428 + static int do_prlimit(struct task_struct *tsk, unsigned int resource, 1429 + struct rlimit *new_rlim, struct rlimit *old_rlim) 1430 + { 1431 + struct rlimit *rlim; 1432 + int retval = 0; 1433 + 1434 + if (resource >= RLIM_NLIMITS) 1435 + return -EINVAL; 1436 + if (new_rlim) { 1437 + if (new_rlim->rlim_cur > new_rlim->rlim_max) 1438 + return -EINVAL; 1439 + if (resource == RLIMIT_NOFILE && 1440 + new_rlim->rlim_max > sysctl_nr_open) 1441 + return -EPERM; 1442 + } 1443 + 1444 + /* protect tsk->signal and tsk->sighand from disappearing */ 1445 + read_lock(&tasklist_lock); 1446 + if (!tsk->sighand) { 1447 + retval = -ESRCH; 1448 + goto out; 1449 + } 1450 + 1451 + rlim = tsk->signal->rlim + resource; 1452 + task_lock(tsk->group_leader); 1453 + if (new_rlim) { 1454 + /* 1455 + * Keep the capable check against init_user_ns until cgroups can 1456 + * contain all limits. 1457 + */ 1458 + if (new_rlim->rlim_max > rlim->rlim_max && 1459 + !capable(CAP_SYS_RESOURCE)) 1460 + retval = -EPERM; 1461 + if (!retval) 1462 + retval = security_task_setrlimit(tsk, resource, new_rlim); 1463 + } 1464 + if (!retval) { 1465 + if (old_rlim) 1466 + *old_rlim = *rlim; 1467 + if (new_rlim) 1468 + *rlim = *new_rlim; 1469 + } 1470 + task_unlock(tsk->group_leader); 1471 + 1472 + /* 1473 + * RLIMIT_CPU handling. Arm the posix CPU timer if the limit is not 1474 + * infinite. In case of RLIM_INFINITY the posix CPU timer code 1475 + * ignores the rlimit. 1476 + */ 1477 + if (!retval && new_rlim && resource == RLIMIT_CPU && 1478 + new_rlim->rlim_cur != RLIM_INFINITY && 1479 + IS_ENABLED(CONFIG_POSIX_TIMERS)) 1480 + update_rlimit_cpu(tsk, new_rlim->rlim_cur); 1481 + out: 1482 + read_unlock(&tasklist_lock); 1483 + return retval; 1484 + } 1485 + 1427 1486 SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim) 1428 1487 { 1429 1488 struct rlimit value; ··· 1624 1565 rlim->rlim_max = RLIM_INFINITY; 1625 1566 else 1626 1567 rlim->rlim_max = (unsigned long)rlim64->rlim_max; 1627 - } 1628 - 1629 - /* make sure you are allowed to change @tsk limits before calling this */ 1630 - int do_prlimit(struct task_struct *tsk, unsigned int resource, 1631 - struct rlimit *new_rlim, struct rlimit *old_rlim) 1632 - { 1633 - struct rlimit *rlim; 1634 - int retval = 0; 1635 - 1636 - if (resource >= RLIM_NLIMITS) 1637 - return -EINVAL; 1638 - if (new_rlim) { 1639 - if (new_rlim->rlim_cur > new_rlim->rlim_max) 1640 - return -EINVAL; 1641 - if (resource == RLIMIT_NOFILE && 1642 - new_rlim->rlim_max > sysctl_nr_open) 1643 - return -EPERM; 1644 - } 1645 - 1646 - /* protect tsk->signal and tsk->sighand from disappearing */ 1647 - read_lock(&tasklist_lock); 1648 - if (!tsk->sighand) { 1649 - retval = -ESRCH; 1650 - goto out; 1651 - } 1652 - 1653 - rlim = tsk->signal->rlim + resource; 1654 - task_lock(tsk->group_leader); 1655 - if (new_rlim) { 1656 - /* Keep the capable check against init_user_ns until 1657 - cgroups can contain all limits */ 1658 - if (new_rlim->rlim_max > rlim->rlim_max && 1659 - !capable(CAP_SYS_RESOURCE)) 1660 - retval = -EPERM; 1661 - if (!retval) 1662 - retval = security_task_setrlimit(tsk, resource, new_rlim); 1663 - } 1664 - if (!retval) { 1665 - if (old_rlim) 1666 - *old_rlim = *rlim; 1667 - if (new_rlim) 1668 - *rlim = *new_rlim; 1669 - } 1670 - task_unlock(tsk->group_leader); 1671 - 1672 - /* 1673 - * RLIMIT_CPU handling. Arm the posix CPU timer if the limit is not 1674 - * infinite. In case of RLIM_INFINITY the posix CPU timer code 1675 - * ignores the rlimit. 1676 - */ 1677 - if (!retval && new_rlim && resource == RLIMIT_CPU && 1678 - new_rlim->rlim_cur != RLIM_INFINITY && 1679 - IS_ENABLED(CONFIG_POSIX_TIMERS)) 1680 - update_rlimit_cpu(tsk, new_rlim->rlim_cur); 1681 - out: 1682 - read_unlock(&tasklist_lock); 1683 - return retval; 1684 1568 } 1685 1569 1686 1570 /* rcu lock must be held */