Question 1
Link to questionA system administrator is tasked with deploying a novel, custom-built web server application on a Red Hat Enterprise Linux system. The executable is located at `/usr/local/sbin/my_web_app`. To ensure proper SELinux operation and integration with the system\'s security policy, the administrator must assign it an appropriate context that allows it to function as a web server. They have determined that the `httpd_exec_t` type is the most suitable for this executable. What is the most effective and persistent method to ensure the `my_web_app` executable and its associated files are correctly labeled with `httpd_exec_t` and that this labeling is maintained across system reboots and policy updates?
Use `semanage fcontext` to define the file context for `/usr/local/sbin/my_web_app` and then use `restorecon` to apply it.
Utilize `semanage port` to associate the `httpd_exec_t` type with the port the web server listens on.
Employ `chcon -Rv --type=httpd_exec_t /usr/local/sbin/my_web_app` to temporarily set the context.
Disable SELinux enforcement using `setenforce 0` for the duration of the application's operation.
Show answer and explanation
Correct answer
Use `semanage fcontext` to define the file context for `/usr/local/sbin/my_web_app` and then use `restorecon` to apply it.
The core of this question revolves around understanding how SELinux policy modules interact with system services, specifically focusing on the implications of context changes and the role of `semanage` and `restorecon`. When a new service, such as a custom web server, is introduced, it needs an appropriate SELinux context to operate. If the service binary is placed in a standard location like `/usr/local/sbin`, SELinux might not have a predefined type for it. The `semanage fcontext` command is used to define a new file context mapping, associating a regular expression with a specific SELinux type. For instance, `semanage fcontext -a -t httpd_exec_t \"/usr/local/sbin/custom_webserver(/.*)?\"` would label the `custom_webserver` executable and any files within its directory structure with the `httpd_exec_t` type, which is typically associated with web server processes. Following this definition, `restorecon -Rv /usr/local/sbin/custom_webserver` is crucial. This command recursively applies the defined SELinux file contexts to the specified path. Without `restorecon`, the new context defined by `semanage` would not be actively enforced on the filesystem until the next system relabeling. Therefore, the correct sequence involves defining the context and then restoring it to make it active. Option b is incorrect because `semanage port` is for network ports, not file contexts. Option c is incorrect as `chcon` is a temporary change and not persistent across reboots or relabels. Option d is incorrect because `setenforce 0` disables SELinux enforcement, which is not the goal of correctly configuring a service\'s context. The proper application of SELinux policy involves persistent context definitions and their subsequent application to files and directories.
