Blame SOURCES/ntp-4.2.6p5-shmperm.patch

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

6e16f6
 
6e16f6
         

Description

6e16f6
-        

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

6e16f6
+        

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.

6e16f6
+
6e16f6
 
6e16f6
         

Structure of shared memory-segment

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

The 'mode' word

6e16f6
+    
6e16f6
+    

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

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

Fudge Factors

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

Additional Information

6e16f6
-            

Reference Clock Drivers

6e16f6
         
6e16f6
+
6e16f6
+	

Public vs. Private SHM segments

6e16f6
+
6e16f6
+	

The driver attempts to create a shared memory segment with an

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

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

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

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

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

    6e16f6
    +	  
    6e16f6
    +	
    6e16f6
    +	
    6e16f6
    +	

    There's no support for POSIX shared memory yet.

    6e16f6
    +
    6e16f6
    +	

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

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

    6e16f6
    +	
    6e16f6
    +	

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

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

    6e16f6
    +
    6e16f6
    +        

    Additional Information

    6e16f6
    +        

    Reference Clock Drivers

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