Great question! Let's clarify how SELinux context and directory permissions work in this scenario.
The Problem
- PostgreSQL is configured to log via rsyslog to a non-standard location (not
/var/log/).
- SELinux blocks log file creation because the directory (and possibly its parents) lack the expected SELinux context.
- You want to avoid recursively relabeling all parent directories (e.g., the whole mount point) to
var_log_t.
- You're considering labeling just the log directory (e.g.,
/custom/log/dir) with syslogd_var_lib_t instead.
Key SELinux Concepts
- SELinux context on a directory controls what processes can create files/directories within it.
- Parent directories must have a context that allows the process (here,
rsyslogd) to create new files or subdirectories.
- The context of the file itself is determined by the context of the parent directory and the SELinux policy.
Your Question
Can this be solved by just applying another SELinux context syslogd_var_lib_t ONLY to the folder where the logs are being stored and not all parent folders as well?
Short Answer
No, not always.
You must ensure that all parent directories up to the mount point have a context that allows the rsyslogd process to traverse (search) and, if needed, create files/directories within the target directory.
- You do NOT need to relabel the entire mount point recursively.
- You DO need to ensure that:
- The log directory itself has the correct context (e.g.,
syslogd_var_lib_t or var_log_t).
- All parent directories have a context that allows
rsyslogd to traverse them (usually system_u:object_r:default_t:s0 is sufficient for traversal, but not for file creation).
Practical Solution
-
Label the log directory (not the whole mount point) with the appropriate context:
semanage fcontext -a -t syslogd_var_lib_t "/custom/log/dir(/.*)?"
restorecon -Rv /custom/log/dir
-
Parent directories (e.g., /custom, /custom/log) do not need to be labeled syslogd_var_lib_t unless rsyslogd needs to create files or directories directly in them.
- They must have a context that allows
rsyslogd to traverse them (usually default_t is fine).
-
If SELinux still blocks access, check the audit logs (/var/log/audit/audit.log) for AVC denials.
- If you see
search or mkdir denials on parent directories, you may need to adjust their context to something less restrictive (but not necessarily var_log_t or syslogd_var_lib_t).
Example
Suppose your log path is /mnt/data/logs/pg/.
- Label
/mnt/data/logs/pg as syslogd_var_lib_t.
- Ensure
/mnt, /mnt/data, /mnt/data/logs have a context that allows traversal (usually default_t).
- Do not recursively relabel
/mnt or /mnt/data as syslogd_var_lib_t or var_log_t—that would be overkill and potentially insecure.
Summary Table
| Directory | SELinux Context | Reason |
|---|
/mnt | default_t | Just needs to be traversable |
/mnt/data | default_t | Just needs to be traversable |
/mnt/data/logs | default_t | Just needs to be traversable |
/mnt/data/logs/pg | syslogd_var_lib_t | Needs to allow log file creation |
In summary:
- Label only the log directory with
syslogd_var_lib_t.
- Parent directories just need to be traversable, not relabeled.
- Do not recursively relabel the whole mount point.
- Check audit logs if you still have issues.
If you want to be extra sure, you can use ls -Zd to check the context of each directory in the path.
If you encounter specific AVC denials, paste them here and I can help interpret and suggest the minimal context changes needed!