Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 99713 - ntpd hits RLIMIT_MEMLOCK limit
Summary: ntpd hits RLIMIT_MEMLOCK limit
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: High trivial (vote)
Assignee: SpanKY
URL: http://lists.ntp.isc.org/pipermail/ha...
Whiteboard:
Keywords:
: 117910 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-07-20 12:02 UTC by Marcin Deranek
Modified: 2006-01-05 11:02 UTC (History)
5 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
added rlimit-memlock.patch (ntp-4.2.0.20040617-r2.ebuild.patch,441 bytes, patch)
2005-08-14 13:20 UTC, Christoph Gysin
Details | Diff
patch to increase the memory lock limit (rlimit-memlock.patch,736 bytes, patch)
2005-08-14 13:23 UTC, Christoph Gysin
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Marcin Deranek 2005-07-20 12:02:45 UTC
In /var/log/grsec.log I got plenty of these:
Jul 19 10:07:40 ftp grsec: denied resource overstep by requesting 4071424 for
RLIMIT_MEMLOCK against limit 32768 for /usr/sbin/ntpd[ntpd:7157] uid/euid:123/123
gid/egid:123/123, parent /sbin/init[init:1] uid/euid:0/0 gid/egid:0/0

Reproducible: Always
Steps to Reproduce:
1. compile kernel with grsec support (hardened-sources-2.6.11-r15) and use it
2. run ntpd
2. watch grsec logfile
Actual Results:  
Entries "...grsec: denied resource overstep by requesting 4071424 for
RLIMIT_MEMLOCK against limit 32768..." appear every hour after some time

Expected Results:  
ntpd should not produce these entries in grsec logfile

ntpd runs with the following options:
/usr/sbin/ntpd -p /var/run/ntpd.pid -u ntp:ntp -i /var/chroot/ntp
I tried without chroot - result is the same
Look at provided URL to get more information about the problem + patch
Comment 1 Karl-Johan Karlsson 2005-08-11 11:52:07 UTC
I get a 404 on that URL. Perhaps you mean <http://lists.ntp.isc.org/pipermail/
hackers/2005-June/001418.html>?
Comment 2 Marcin Deranek 2005-08-13 06:26:27 UTC
(In reply to comment #1)
> I get a 404 on that URL. Perhaps you mean <http://lists.ntp.isc.org/pipermail/
> hackers/2005-June/001418.html>?

Here is the copy of the post to ntp:hackers list:

[ntp:hackers] [PATCH] - ntpd Exiting:out of memory.
Jim Houston jim.houston at comcast.net
Wed Jun 15 09:39:34 PDT 2005

    * Previous message: [ntp:hackers] Standardizing NTP...
    * Next message: [ntp:hackers] NTP application status
    * Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

Hi Everyone,

When I run ntpd on Linux and use the drop root option I almost
always get a "Exiting:out of memory" failure.

This is caused by Linux setting a very small default value
for RLIMIT_MEMLOCK.  On the RH EL3 systems its only 32K bytes.
This means that any malloc call after ntpd drops root privlege
are likely to fail because the RLIMIT_MEMLOCK limit prevents
locking more memory.

The attached patch sets both the rl_cur and rl_max values to 
32M bytes.  To be useful this value has to be larger than the 
largest ntpd resident set size.  I checked ntpd built for an
AMD opteron.  It was using 19M bytes.

Jim Houston - Concurrent Computer Corp.

--- ntp-stable-4.2.0a-20040617/ntpd/ntpd.c.0	2005-06-14 12:02:43.000000000 -0400
+++ ntp-stable-4.2.0a-20040617/ntpd/ntpd.c	2005-06-15 12:12:13.253809589 -0400
@@ -691,6 +691,16 @@
 				"Cannot adjust stack limit for mlockall: %m");
 		    }
 	    }
+	    /*
+	     * The default RLIMIT_MEMLOCK is very low on Linux systems.
+	     * Unless we increase this limit malloc calls are likely to
+	     * fail if we drop root privlege.  To be useful the value
+	     * has to be larger than the largest ntpd resident set size.
+	     */
+	    rl.rlim_cur = rl.rlim_max = 32*1024*1024;
+	    if (setrlimit(RLIMIT_MEMLOCK, &rl) == -1) {
+	    	msyslog(LOG_ERR, "Cannot set RLIMIT_MEMLOCK: %m");
+	    }
 	}
 # endif /* HAVE_SETRLIMIT */
 	/*
Comment 3 Karl-Johan Karlsson 2005-08-14 01:55:09 UTC
I've now been running this patch for a day, and it seems to solve the problem 
for me. However, 32MB seems excessive - my ntpd process is only 7MB, so I set 
the limit to 10MB. My local NTP master server is very short on physical memory, 
and a runaway ntpd that locked the full 32MB would not be good.
Comment 4 Marcin Deranek 2005-08-14 03:43:42 UTC
(In reply to comment #3)
> I've now been running this patch for a day, and it seems to solve the problem 
> for me. However, 32MB seems excessive - my ntpd process is only 7MB, so I set 
> the limit to 10MB. My local NTP master server is very short on physical memory, 
> and a runaway ntpd that locked the full 32MB would not be good.

I don't think it will lock the whole 32MB. We only set maximum allowed size to
32MB however process will lock only the amount it actually uses.
Extracted from mlockall manual page:

mlockall() locks all of the calling process's virtual address space into RAM,
preventing that memory from being paged to the swap area.

This means that we should set RLIMIT_MEMLOCK to something above the highest
possible value and amount of locked memory will depend on how much virtual
memory process actually consumes.
On my i686 (-Os) ntpd consumes ~4MB and on athlon64 (-O3) ntpd consumes ~16MB.
Comment 5 Christoph Gysin 2005-08-14 13:20:42 UTC
Created attachment 65963 [details, diff]
added rlimit-memlock.patch
Comment 6 Christoph Gysin 2005-08-14 13:23:28 UTC
Created attachment 65964 [details, diff]
patch to increase the memory lock limit

the patch posted from Marcin Deranekl
Comment 7 Christoph Gysin 2005-08-14 13:24:34 UTC
Added the patch as attachement and adjusted the ebuild from stable accordingly.

Could anybody test and confirm if it works? Thanks.
Comment 8 SpanKY gentoo-dev 2005-08-14 19:39:06 UTC
this really needs to go through upstream ntp first

https://ntp.isc.org/bugs/
Comment 9 Christoph Gysin 2005-08-15 00:27:20 UTC
posted on ntp.isc.org:
https://ntp.isc.org/bugs/show_bug.cgi?id=477

Will report back if upstream has accepted it.

@vapier:
Shouldn't this get patched in portage until upstream releases a new release?
Comment 10 Christoph Gysin 2005-08-15 02:21:06 UTC
Okay, that was fast.

It's fixed in the ntp cvs, and will be in the 4.2.1 release.
(see https://ntp.isc.org/bugs/show_bug.cgi?id=477)

Could we fix it with the patch from #6 until 4.2.1 is out?
Comment 11 SpanKY gentoo-dev 2005-08-15 20:24:19 UTC
added to ntp-4.2.0.20050303, thanks for doing all the hard work :)
Comment 12 Robert Paskowitz (RETIRED) gentoo-dev 2006-01-05 10:55:53 UTC
*** Bug 117910 has been marked as a duplicate of this bug. ***
Comment 13 Robert Paskowitz (RETIRED) gentoo-dev 2006-01-05 10:56:50 UTC
Looks like this has re-entered upstream, anyone care to re-open there (so I don't need to open another bugzilla account)? Thanks,
Comment 14 Robert Paskowitz (RETIRED) gentoo-dev 2006-01-05 11:00:27 UTC
Ugh, sorry, misread some versions, has nothing to do with upstream, this is just about the bug occuring in an earlier version, 20050303 is still in ~arch. Looks like patch for it works with 20040617-r3 though, so should be a quick fix, or 20050303 could be marked stable.
Comment 15 SpanKY gentoo-dev 2006-01-05 11:02:55 UTC
file a new bug for ntp stabilization