@@ -109,11 +109,21 @@ int mac_selinux_init(const char *prefix) {
109109 return r ;
110110}
111111
112+ void mac_selinux_finish (void ) {
113+
114+ #ifdef HAVE_SELINUX
115+ if (!label_hnd )
116+ return ;
117+
118+ selabel_close (label_hnd );
119+ #endif
120+ }
121+
112122int mac_selinux_fix (const char * path , bool ignore_enoent , bool ignore_erofs ) {
113- int r = 0 ;
114123
115124#ifdef HAVE_SELINUX
116125 struct stat st ;
126+ int r ;
117127
118128 assert (path );
119129
@@ -148,22 +158,31 @@ int mac_selinux_fix(const char *path, bool ignore_enoent, bool ignore_erofs) {
148158 if (ignore_erofs && errno == EROFS )
149159 return 0 ;
150160
151- log_enforcing ("Unable to fix SELinux label of %s: %m" , path );
152- r = security_getenforce () == 1 ? - errno : 0 ;
161+ log_enforcing ("Unable to fix SELinux security context of %s: %m" , path );
162+ if (security_getenforce () == 1 )
163+ return - errno ;
153164 }
154165#endif
155166
156- return r ;
167+ return 0 ;
157168}
158169
159- void mac_selinux_finish ( void ) {
170+ int mac_selinux_apply ( const char * path , const char * label ) {
160171
161172#ifdef HAVE_SELINUX
162- if (! label_hnd )
163- return ;
173+ assert ( path );
174+ assert ( label ) ;
164175
165- selabel_close (label_hnd );
176+ if (!mac_selinux_use ())
177+ return 0 ;
178+
179+ if (setfilecon (path , (security_context_t ) label ) < 0 ) {
180+ log_enforcing ("Failed to set SELinux security context %s on path %s: %m" , label , path );
181+ if (security_getenforce () == 1 )
182+ return - errno ;
183+ }
166184#endif
185+ return 0 ;
167186}
168187
169188int mac_selinux_get_create_label_from_exe (const char * exe , char * * label ) {
@@ -279,12 +298,24 @@ int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, char **label
279298 return r ;
280299}
281300
282- int mac_selinux_context_set (const char * path , mode_t mode ) {
301+ void mac_selinux_free (char * label ) {
302+
303+ #ifdef HAVE_SELINUX
304+ if (!mac_selinux_use ())
305+ return ;
306+
307+ freecon ((security_context_t ) label );
308+ #endif
309+ }
310+
311+ int mac_selinux_create_file_prepare (const char * path , mode_t mode ) {
283312 int r = 0 ;
284313
285314#ifdef HAVE_SELINUX
286315 _cleanup_security_context_free_ security_context_t filecon = NULL ;
287316
317+ assert (path );
318+
288319 if (!label_hnd )
289320 return 0 ;
290321
@@ -294,7 +325,7 @@ int mac_selinux_context_set(const char *path, mode_t mode) {
294325 else if (r == 0 ) {
295326 r = setfscreatecon (filecon );
296327 if (r < 0 ) {
297- log_enforcing ("Failed to set SELinux file context on %s: %m" , path );
328+ log_enforcing ("Failed to set SELinux security context %s for %s: %m" , filecon , path );
298329 r = - errno ;
299330 }
300331 }
@@ -306,24 +337,7 @@ int mac_selinux_context_set(const char *path, mode_t mode) {
306337 return r ;
307338}
308339
309- int mac_selinux_socket_set (const char * label ) {
310-
311- #ifdef HAVE_SELINUX
312- if (!mac_selinux_use ())
313- return 0 ;
314-
315- if (setsockcreatecon ((security_context_t ) label ) < 0 ) {
316- log_enforcing ("Failed to set SELinux context (%s) on socket: %m" , label );
317-
318- if (security_getenforce () == 1 )
319- return - errno ;
320- }
321- #endif
322-
323- return 0 ;
324- }
325-
326- void mac_selinux_context_clear (void ) {
340+ void mac_selinux_create_file_clear (void ) {
327341
328342#ifdef HAVE_SELINUX
329343 PROTECT_ERRNO ;
@@ -335,37 +349,49 @@ void mac_selinux_context_clear(void) {
335349#endif
336350}
337351
338- void mac_selinux_socket_clear ( void ) {
352+ int mac_selinux_create_socket_prepare ( const char * label ) {
339353
340354#ifdef HAVE_SELINUX
341- PROTECT_ERRNO ;
342-
343355 if (!mac_selinux_use ())
344- return ;
356+ return 0 ;
345357
346- setsockcreatecon (NULL );
358+ assert (label );
359+
360+ if (setsockcreatecon ((security_context_t ) label ) < 0 ) {
361+ log_enforcing ("Failed to set SELinux security context %s for sockets: %m" , label );
362+
363+ if (security_getenforce () == 1 )
364+ return - errno ;
365+ }
347366#endif
367+
368+ return 0 ;
348369}
349370
350- void mac_selinux_free ( const char * label ) {
371+ void mac_selinux_create_socket_clear ( void ) {
351372
352373#ifdef HAVE_SELINUX
374+ PROTECT_ERRNO ;
375+
353376 if (!mac_selinux_use ())
354377 return ;
355378
356- freecon (( security_context_t ) label );
379+ setsockcreatecon ( NULL );
357380#endif
358381}
359382
360383int mac_selinux_mkdir (const char * path , mode_t mode ) {
361- int r = 0 ;
362384
363- #ifdef HAVE_SELINUX
364385 /* Creates a directory and labels it according to the SELinux policy */
386+
387+ #ifdef HAVE_SELINUX
365388 _cleanup_security_context_free_ security_context_t fcon = NULL ;
389+ int r ;
390+
391+ assert (path );
366392
367393 if (!label_hnd )
368- return 0 ;
394+ goto skipped ;
369395
370396 if (path_is_absolute (path ))
371397 r = selabel_lookup_raw (label_hnd , & fcon , path , S_IFDIR );
@@ -383,7 +409,7 @@ int mac_selinux_mkdir(const char *path, mode_t mode) {
383409 r = setfscreatecon (fcon );
384410
385411 if (r < 0 && errno != ENOENT ) {
386- log_enforcing ("Failed to set security context %s for %s: %m" , fcon , path );
412+ log_enforcing ("Failed to set SELinux security context %s for %s: %m" , fcon , path );
387413
388414 if (security_getenforce () == 1 ) {
389415 r = - errno ;
@@ -397,9 +423,11 @@ int mac_selinux_mkdir(const char *path, mode_t mode) {
397423
398424finish :
399425 setfscreatecon (NULL );
400- #endif
401-
402426 return r ;
427+
428+ skipped :
429+ #endif
430+ return mkdir (path , mode ) < 0 ? - errno : 0 ;
403431}
404432
405433int mac_selinux_bind (int fd , const struct sockaddr * addr , socklen_t addrlen ) {
@@ -416,7 +444,7 @@ int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
416444 assert (addr );
417445 assert (addrlen >= sizeof (sa_family_t ));
418446
419- if (!mac_selinux_use () || ! label_hnd )
447+ if (!label_hnd )
420448 goto skipped ;
421449
422450 /* Filter out non-local sockets */
@@ -450,7 +478,7 @@ int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
450478 r = setfscreatecon (fcon );
451479
452480 if (r < 0 && errno != ENOENT ) {
453- log_enforcing ("Failed to set security context %s for %s: %m" , fcon , path );
481+ log_enforcing ("Failed to set SELinux security context %s for %s: %m" , fcon , path );
454482
455483 if (security_getenforce () == 1 ) {
456484 r = - errno ;
@@ -470,15 +498,3 @@ int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) {
470498#endif
471499 return bind (fd , addr , addrlen ) < 0 ? - errno : 0 ;
472500}
473-
474- int mac_selinux_apply (const char * path , const char * label ) {
475- int r = 0 ;
476-
477- #ifdef HAVE_SELINUX
478- if (!mac_selinux_use ())
479- return 0 ;
480-
481- r = setfilecon (path , (char * )label );
482- #endif
483- return r ;
484- }
0 commit comments