CodexBloom - Programming Q&A Platform

CentOS 7 - SELinux Blocking Node.js Application from Reading Files

👀 Views: 21 💬 Answers: 1 📅 Created: 2025-06-10
linux node.js selinux JavaScript

I've been struggling with this for a few days now and could really use some help... I've encountered a strange issue with I'm testing a new approach and I've been struggling with this for a few days now and could really use some help..... Hey everyone, I'm running into an issue that's driving me crazy. I'm running a Node.js application on CentOS 7, and I’m working with problems with file access due to SELinux configuration. My application tries to read a configuration file located at `/etc/myapp/config.json`, but it consistently fails with the behavior message: ``` behavior: ENOENT: no such file or directory, open '/etc/myapp/config.json' ``` I've verified that the file exists and that the Node.js application has the correct permissions to read it. When I check the SELinux status with `sestatus`, it shows that SELinux is enabled and enforcing: ``` SELinux status: enabled SELinuxfs mount: /selinux SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: enforcing ``` To troubleshoot, I used the `audit2allow` command to check if SELinux is denying access. After running `ausearch -m avc -ts recent` and parsing the logs, I see entries like this: ``` type=AVC msg=audit(1668723346.456:123): avc: denied { read } for pid=1234 comm="node" path="/etc/myapp/config.json" dev="dm-0" ino=1234567 scontext=system_u:system_r:node_t:s0 tcontext=system_u:object_r:etc_t:s0 tclass=file ``` This indicates that the Node.js process, which is labeled `node_t`, is trying to access a file labeled `etc_t`. I tried changing the SELinux context of the file with the following command: ``` chcon -t node_exec_t /etc/myapp/config.json ``` However, it didn’t resolve the scenario. I also considered switching SELinux to permissive mode using `setenforce 0`, and the application worked as expected, but I don't want to run my server with SELinux in permissive mode due to security concerns. What would be the recommended way to allow my Node.js application to read the configuration file while keeping SELinux enforcing? Should I create a custom policy, or is there a better method to manage file access permissions in this context? This is part of a larger CLI tool I'm building. Has anyone else encountered this? What's the best practice here? For reference, this is a production service. Am I approaching this the right way? What's the correct way to implement this? I've been using Javascript for about a year now. Any help would be greatly appreciated!