Bug 235122 - rc.subr limits call breaks non-root usage
Summary: rc.subr limits call breaks non-root usage
Status: In Progress
Alias: None
Product: Base System
Classification: Unclassified
Component: conf (show other bugs)
Version: 11.2-RELEASE
Hardware: Any Any
: --- Affects Some People
Assignee: Mateusz Piotrowski
URL:
Keywords: feature, needs-qa, regression
Depends on:
Blocks: 262170
  Show dependency treegraph
 
Reported: 2019-01-22 09:46 UTC by Markus Wild
Modified: 2022-07-26 14:42 UTC (History)
2 users (show)

See Also:
koobs: mfc-stable12?


Attachments
patch to allow non-root bypass of call to limits (567 bytes, patch)
2019-01-22 09:59 UTC, Markus Wild
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Wild 2019-01-22 09:46:10 UTC
We sometimes want customers to be able to restart services (like ldap) 
running on non-standard and non-privileged ports. With the new unconditional 
call to "limits" in the rc.subr start function, this fails:

$ sh /usr/local/etc/rc.d/slapd start
Starting slapd.
limits: setrlimit datasize: Operation not permitted

I suggest a change like the following:

--- /etc/rc.subr.orig   2019-01-22 10:40:13.973245000 +0100
+++ /etc/rc.subr        2019-01-22 09:51:18.058288000 +0100
@@ -1073,7 +1073,9 @@
                        fi
 
                                        # Prepend default limits
-                       _doit="$_cd limits -C $_login_class $_doit"
+                       if [ `/usr/bin/id -u` -eq 0 ]; then
+                               _doit="$_cd limits -C $_login_class $_doit"
+                       fi
 
                                        # run the full command
                                        #

and the same service can now be maintained by a non privileged user:

$ sh /usr/local/etc/rc.d/slapd start
Starting slapd.

Kind regards,
Markus
Comment 1 Kubilay Kocak freebsd_committer freebsd_triage 2019-01-22 09:53:55 UTC
Thank you for the report and patch Markus. Could you please include your patch as an attachment?
Comment 2 Markus Wild 2019-01-22 09:59:18 UTC
Created attachment 201326 [details]
patch to allow non-root bypass of call to limits

Here's the patch as attachment.
Comment 3 Conrad Meyer freebsd_committer freebsd_triage 2019-01-22 16:58:39 UTC
This change comes with the caveat that services started by non-privileged users will silently have different, non-default limits which may impact the function of the service.
Comment 4 Markus Wild 2019-01-23 08:15:41 UTC
(In reply to Conrad Meyer from comment #3)
services started by non privileged users will inherit the limits
of those users, which at least in my understanding is working as intended.
An alternative approach would be to have a generic _limit_enable variable
per service, in the same spirit as services such as apache used to have
before there was central support for service limits. My approach was the
one with the least amount of needed changes and potential impact to existing
setups (considering that there was no central limits support previously).
Comment 5 Mateusz Piotrowski freebsd_committer freebsd_triage 2021-01-04 18:24:11 UTC
I think that I've accidentally discovered a hack/workaround:

    $ limits -C default echo ok
    limits: setrlimit datasize: Operation not permitted
    $ limits -C nonexistent echo ok
    login class 'nonexistent' non-existent, using default
    limits: setrlimit datasize: Operation not permitted
    $ limits -C me echo ok
    login class 'me' non-existent, using current settings
    ok

It turns out that setting the login class to "me" causes limits(1) to only print a warning, instead of failing. I am not sure why it is so. Please note that I do in fact have a "me" entry in my ~/.login_conf.
Comment 6 Mateusz Piotrowski freebsd_committer freebsd_triage 2022-07-08 16:44:03 UTC
Another workaround that works on recent 14.0-CURRENT is to redefine limits(1) in the service script as follows:

        limits() {
        	shift 2
        	"$@"
        }
Comment 7 Mateusz Piotrowski freebsd_committer freebsd_triage 2022-07-08 16:45:34 UTC
I'm trying to land a patch to address this issue: https://reviews.freebsd.org/D33381