@@ -113,10 +113,25 @@ static int add(const char *name, struct lyd_node *cif)
113113
114114 /* Content mount: create a unique file with 'content' and bind mount */
115115 if (data ) {
116+ const char * mode = lydx_get_cattr (node , "mode" );
116117 const char * contdir = "/run/containers/files" ;
118+ mode_t file_mode = 0644 ;
117119 char cmd [256 ];
120+ int pos , fd ;
118121 FILE * pp ;
119- int pos ;
122+
123+ if (mode ) {
124+ unsigned long val ;
125+ char * endptr ;
126+
127+ val = strtoul (mode , & endptr , 8 );
128+ if (* endptr != '\0' || val > 07777 ) {
129+ ERROR ("%s: invalid file mode '%s'" , nm , mode );
130+ continue ;
131+ }
132+
133+ file_mode = (mode_t )val ;
134+ }
120135
121136 /*
122137 * prefix file name with container name, shared namespace,
@@ -129,6 +144,27 @@ static int add(const char *name, struct lyd_node *cif)
129144 nm [i ] = '-' ;
130145 }
131146
147+ /*
148+ * Always create with secure permissions, then immediately
149+ * set final mode. This takes care of both new files and
150+ * updates to existing files atomically.
151+ */
152+ fd = open (nm , O_CREAT | O_WRONLY | O_TRUNC , 0600 );
153+ if (fd < 0 ) {
154+ ERRNO ("%s: failed creating file %s" , name , nm );
155+ continue ;
156+ }
157+
158+ /* Set final permissions */
159+ if (fchmod (fd , file_mode ) < 0 ) {
160+ ERRNO ("%s: failed setting file mode %s" , nm , mode );
161+ close (fd );
162+ unlink (nm );
163+ continue ;
164+ }
165+ close (fd );
166+
167+ /* Now decode base64 content into the properly secured file */
132168 snprintf (cmd , sizeof (cmd ), "base64 -d > %s" , nm );
133169 pp = popen (cmd , "w" );
134170 if (!pp || fputs (data , pp ) < 0 ) {
@@ -137,8 +173,8 @@ static int add(const char *name, struct lyd_node *cif)
137173 pclose (pp );
138174 continue ;
139175 }
140-
141176 pclose (pp );
177+
142178 type = "bind" ; /* discard any configured setting */
143179 src = nm ; /* discard any source, not used for content mounts */
144180 }
0 commit comments