Blame SOURCES/ntp-4.2.6p5-shmperm.patch

6d3098
diff -up ntp-4.2.6p5/html/drivers/driver28.html.shmperm ntp-4.2.6p5/html/drivers/driver28.html
6d3098
--- ntp-4.2.6p5/html/drivers/driver28.html.shmperm	2009-12-09 08:36:37.000000000 +0100
6d3098
+++ ntp-4.2.6p5/html/drivers/driver28.html	2015-02-09 15:57:57.450877311 +0100
6d3098
@@ -18,7 +18,8 @@
6d3098
             Driver ID: <tt>SHM</tt>

6d3098
 
6d3098
         

Description

6d3098
-        

This driver receives its reference clock info from a shared memory-segment. The shared memory-segment is created with owner-only access for unit 0 and 1, and world access for unit 2 and 3

6d3098
+        

This driver receives its reference clock info from a shared memory-segment. The shared memory-segment is created with owner-only access for unit 0 and 1, and world access for other units unless the mode word is set for owner-only access.

6d3098
+
6d3098
 
6d3098
         

Structure of shared memory-segment

6d3098
         
struct shmTime {
6d3098
@@ -94,6 +95,40 @@ Here is a sample showing the GPS recepti
6d3098
 54364 85700.160 127.127.28.0  65   0  65   0   0
6d3098
 
6d3098
 
6d3098
+    

The 'mode' word

6d3098
+    
6d3098
+    

6d3098
+      Some aspects of the driver behavior can be adjusted by setting bits of
6d3098
+      the 'mode' word in the server configuration line:
6d3098
+        <tt>server 127.127.28.</tt>x<tt> mode </tt>Y
6d3098
+    

6d3098
+
6d3098
+    
6d3098
+      <caption>mode word bits and bit groups</caption>
6d3098
+      
6d3098
+	Bit
6d3098
+	Dec
6d3098
+	Hex
6d3098
+	Meaning
6d3098
+      
6d3098
+      
6d3098
+      
6d3098
+	0
6d3098
+	1
6d3098
+	1
6d3098
+	The SHM segment is private (mode 0600). This is the fixed
6d3098
+	default for clock units 0 and 1; clock units >1 are mode
6d3098
+	0666 unless this bit is set for the specific unit.
6d3098
+
6d3098
+      
6d3098
+	1-31
6d3098
+	-
6d3098
+	-
6d3098
+	reserved -- do not use
6d3098
+	
6d3098
+      
6d3098
+      
6d3098
+    
6d3098
 	

Fudge Factors

6d3098
         
6d3098
             
<tt>time1 time</tt>
6d3098
@@ -112,9 +147,64 @@ Here is a sample showing the GPS recepti
6d3098
             
Not used by this driver.
6d3098
             
<tt>flag4 0 | 1</tt>
6d3098
             
If flag4 is set, clockstats records will be written when the driver is polled.
6d3098
-            

Additional Information

6d3098
-            

Reference Clock Drivers

6d3098
         
6d3098
+
6d3098
+	

Public vs. Private SHM segments

6d3098
+
6d3098
+	

The driver attempts to create a shared memory segment with an

6d3098
+	  identifier depending on the unit number. This identifier (which can be
6d3098
+	  a numeric value or a string) clearly depends on the method used, which
6d3098
+	  in turn depends on the host operating system:

6d3098
+
6d3098
+	
    6d3098
    +	  
  • 6d3098
    +	      <tt>Windows</tt> uses a file mapping to the page file with the
    6d3098
    +	      name '<tt>Global\NTP</tt>u' for public accessible
    6d3098
    +	      mappings, where u is the clock unit. Private /
    6d3098
    +	      non-public mappings are created as
    6d3098
    +	      '<tt>Local\NTP</tt>u'.
    6d3098
    +	    

    6d3098
    +	      Public access assigns a NULL DACL to the memory mapping, while
    6d3098
    +	      private access just uses the default DACL of the process creating
    6d3098
    +	      the mapping.
    6d3098
    +	    

    6d3098
    +	  
    6d3098
    +	  
  • 6d3098
    +	      <tt>SYSV IPC</tt> creates a shared memory segment with a key value
    6d3098
    +	      of <tt>0x4E545030</tt> + u, where u is again
    6d3098
    +	      the clock unit. (This value could be hex-decoded as 'NTP0',
    6d3098
    +	      'NTP1',..., with funny characters for units > 9.)
    6d3098
    +	    

    6d3098
    +	      Public access means a permission set of 0666, while private access
    6d3098
    +	      creates the mapping with a permission set of 0600.
    6d3098
    +	    

    6d3098
    +	  
    6d3098
    +	
    6d3098
    +	
    6d3098
    +	

    There's no support for POSIX shared memory yet.

    6d3098
    +
    6d3098
    +	

    NTPD is started as root on most POSIX-like operating systems

    6d3098
    +	and uses the setuid/setgid system API to run under reduced rights once
    6d3098
    +	the initial setup of the process is done. One consequence out of this
    6d3098
    +	is that the allocation of SHM segments must be done early during the
    6d3098
    +	clock setup. The actual polling of the clock is done as the run-time
    6d3098
    +	user; deferring the creation of the SHM segment to this point will
    6d3098
    +	create a SHM segment owned by the runtime-user account. The internal
    6d3098
    +	structure of NTPD does not permit the use of a fudge flag if
    6d3098
    +	this is to be avoided; this is the reason why a mode bit is used for
    6d3098
    +	the configuration of a public segment.
    6d3098
    +	

    6d3098
    +	
    6d3098
    +	

    When running under Windows, the chosen user account must be able to

    6d3098
    +	create a SHM segment in the global object name space for SHM clocks with
    6d3098
    +	public access. Otherwise the session isolation used by Windows kernels
    6d3098
    +	after WinXP will get into the way if the client program does not run in
    6d3098
    +	the same session.
    6d3098
    +	

    6d3098
    +
    6d3098
    +        

    Additional Information

    6d3098
    +        

    Reference Clock Drivers

    6d3098
    +
    6d3098
             
    6d3098
             <script type="text/javascript" language="javascript" src="scripts/footer.txt"></script>
    6d3098
         </body>
    6d3098
    diff -up ntp-4.2.6p5/ntpd/refclock_shm.c.shmperm ntp-4.2.6p5/ntpd/refclock_shm.c
    6d3098
    --- ntp-4.2.6p5/ntpd/refclock_shm.c.shmperm	2015-02-09 15:52:06.131877933 +0100
    6d3098
    +++ ntp-4.2.6p5/ntpd/refclock_shm.c	2015-02-09 15:52:06.137877933 +0100
    6d3098
    @@ -52,6 +52,11 @@
    6d3098
     #define NSAMPLES        3       /* stages of median filter */
    6d3098
     
    6d3098
     /*
    6d3098
    + * Mode flags
    6d3098
    + */
    6d3098
    +#define SHM_MODE_PRIVATE 0x0001
    6d3098
    +
    6d3098
    +/*
    6d3098
      * Function prototypes
    6d3098
      */
    6d3098
     static  int     shm_start       (int unit, struct peer *peer);
    6d3098
    @@ -99,6 +104,7 @@ struct shmTime {
    6d3098
     
    6d3098
     struct shmunit {
    6d3098
     	struct shmTime *shm;	/* pointer to shared memory segment */
    6d3098
    +	int forall;		/* access for all UIDs?	*/
    6d3098
     
    6d3098
     	/* debugging/monitoring counters - reset when printed */
    6d3098
     	int ticks;		/* number of attempts to read data*/
    6d3098
    @@ -109,9 +115,12 @@ struct shmunit {
    6d3098
     };
    6d3098
     
    6d3098
     
    6d3098
    -struct shmTime *getShmTime(int);
    6d3098
    -
    6d3098
    -struct shmTime *getShmTime (int unit) {
    6d3098
    +static struct shmTime*
    6d3098
    +getShmTime(
    6d3098
    +	int unit,
    6d3098
    +	int/*BOOL*/ forall
    6d3098
    +	)
    6d3098
    +{
    6d3098
     #ifndef SYS_WINNT
    6d3098
     	int shmid=0;
    6d3098
     
    6d3098
    @@ -119,8 +128,8 @@ struct shmTime *getShmTime (int unit) {
    6d3098
     	 * Big units will give non-ascii but that's OK
    6d3098
     	 * as long as everybody does it the same way. 
    6d3098
     	 */
    6d3098
    -	shmid=shmget (0x4e545030+unit, sizeof (struct shmTime), 
    6d3098
    -		      IPC_CREAT|(unit<2?0600:0666));
    6d3098
    +	shmid=shmget(0x4e545030 + unit, sizeof (struct shmTime),
    6d3098
    +		      IPC_CREAT | (forall ? 0666 : 0600));
    6d3098
     	if (shmid==-1) { /*error */
    6d3098
     		msyslog(LOG_ERR,"SHM shmget (unit %d): %s",unit,strerror(errno));
    6d3098
     		return 0;
    6d3098
    @@ -199,7 +208,9 @@ shm_start(
    6d3098
     	memset(up, 0, sizeof(*up));
    6d3098
     	pp->unitptr = (caddr_t)up;
    6d3098
     
    6d3098
    -	up->shm = getShmTime(unit);
    6d3098
    +	up->forall = (unit >= 2) && !(peer->ttl & SHM_MODE_PRIVATE);
    6d3098
    +
    6d3098
    +	up->shm = getShmTime(unit, up->forall);
    6d3098
     
    6d3098
     	/*
    6d3098
     	 * Initialize miscellaneous peer variables
    6d3098
    @@ -314,7 +325,7 @@ int shm_peek(
    6d3098
     	if (up->shm == 0) {
    6d3098
     		/* try to map again - this may succeed if meanwhile some-
    6d3098
     		body has ipcrm'ed the old (unaccessible) shared mem segment */
    6d3098
    -		up->shm = getShmTime(unit);
    6d3098
    +		up->shm = getShmTime(unit, up->forall);
    6d3098
     	}
    6d3098
     	shm = up->shm;
    6d3098
     	if (shm == 0) {