Blame SOURCES/ntp-4.2.6p5-shmperm.patch

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

2b78f7
 
2b78f7
         

Description

2b78f7
-        

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

2b78f7
+        

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.

2b78f7
+
2b78f7
 
2b78f7
         

Structure of shared memory-segment

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

The 'mode' word

2b78f7
+    
2b78f7
+    

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

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

Fudge Factors

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

Additional Information

2b78f7
-            

Reference Clock Drivers

2b78f7
         
2b78f7
+
2b78f7
+	

Public vs. Private SHM segments

2b78f7
+
2b78f7
+	

The driver attempts to create a shared memory segment with an

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

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

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

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

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

    2b78f7
    +	  
    2b78f7
    +	
    2b78f7
    +	
    2b78f7
    +	

    There's no support for POSIX shared memory yet.

    2b78f7
    +
    2b78f7
    +	

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

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

    2b78f7
    +	
    2b78f7
    +	

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

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

    2b78f7
    +
    2b78f7
    +        

    Additional Information

    2b78f7
    +        

    Reference Clock Drivers

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