Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] NGinX, document root, permissions & ownership
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
kgdrenefort
Guru
Guru


Joined: 19 Sep 2023
Posts: 318
Location: Somewhere in the 77

PostPosted: Wed Jun 12, 2024 9:59 am    Post subject: [SOLVED] NGinX, document root, permissions & ownership Reply with quote

Hello,

Goals :

My goal is to have a separated UNIX user (let's say srvadm) to manages data on the DocumentRoot of the future NGinX's virtual host I have to migrate on it. I want to properly manage how nginx user/group would interact with the DR's data (to serve files) aside this users who would add/remove/modify/rename files and directories. As creating new virtual host directories and files hierarchy, without being allowed anything else on the system, beside being able to read logs of VH.

This is how I imagine I should handle this:

1/ Separate from the classic path (/var/www) the DocumentRoot, on another disk, no needs to bind anything to simulate the classic path.

2/ Add a UNIX user, without any permissions on the system, besides:
a) Read, write and execute regarding what it try to do (no need to get execution permission for an index.html… but for directories yes if I want it to go inside !), inside the DocumentRoot only.
b) Not being able to login from a password on the command line or any kind of session (not even TTY), specially not from SSH/sFTP or any remote way (LAN or WAN). Only from the su - command locally, from root user or my every-day user if it's not goofy.
c) Won't be able to do anything beside a) and b), even logs would be moved elsewhere from /var/log and this user would need as well to be able to read them, being able to execute directories (access inside), but no need for write or execute for files.

3/ "other" would have not a single right into the DocumentRoot and logs directories, only root (duh) and this user would be able to access these, not even my every-day user.

My problem:

I do not know what would be the best:

Case I/ :

Set this user as the main owner of the DocumentRoot (srvadm:nginx), allowing it to rwx directories and rw files, allowing aside nginx group permissions to serves external request.

Case II/ :
Set this as group instead of main users (nginx:srvadmin), allowing it the same than I/, but keeping nginx as the main user, and only srvadmin as group.

It might sounds a bit of an overthinking, but I really wonder what would be the best, easiest and most secure way to handle this. I question myself too because I am currently thinking of the defaults user:group as user, group and other permissions. For other, would always be no-rwx (0), don't need it, unsecure too.

To be honest, I am currently writing my own BASH script to create this directory hierarchy inside the root of NGinX with the propers ownership & permissions, because I do not add a site very often and it's easy to forgot what was the good way, how you did it, without taking dirty shortcut.

If solution I/, it would means:
Files handled by user (srvadmn) would need u:rw- (6), to create/remove/modify files as reading them. It does not need to execute anything in this context.
Directories handled by user (srvadmn) would need u:rwx (7), to create/remove/rename these dir, but also get into the directories (so x is needed).

Files handled by group (nginx) would need g:r-- (4), to serves the files as requested on internet, no need to write or execute either.
Directories handled by group (nginx) would need g:rx- (5), to serves files inside them as requested on internet, no need for writing too.

It would mean, in this case, these kind of permissions and ownership:

- srvadm:nginx, files: 6 (rw-) 4 (r--) 0 (---), so 640 (- rw- r-- ---) for files.
- srvadm:nginx, directories: 7 (rwx) 5 (r-x) 0 (---), so 750 (d rwx r-x ---) for directories.

If solution II/, it would means:
Files handled by user (nginx) would need u:r-- (4), to serves files only, without being able to create/remove/rename anything. Or execute, not needed.
Directories handled by user (nginx) would need u:r-x (5), to serves files into these directories by going into them, no need to create/remove/rename anything.

Files handled by group (srvadmn) would need g:rw- (5), to create/remove/modify/rename any files as needed, without being able to execute anything.
Directories handled by group (srvadmn) would need g:rwx (7), to create/remove/modify/rename any directories needed, as going into these directory since it would be needed.

It would mean, in this case, these kind of permissions and ownership:
- nginx:srvadm, files 4 (r--) 6 (rw-) 0 (---), so 460 (- r-- rw- ---) for files.
- nginx:srvadm, directories 5 (r-x) 7 (rwx) 0 (---), so 570 (d r-x rwx ---) for directories.

Résumé:


Case I/:

- User & group: srvadm:nginx
- Files: 640 (- rw- r-- ---)
- Directories: 750 (d rwx r-x ---)

Here the user (srvadm) could read and write on file, but no execution (not needed). It'll be able to have full permissions on directories.

The group (nginx) could only read files, no write and execution. The group could read and execute (going into) directories to be able to reach files to serve.

Other won't be able to do anything, files or group.

Case II/:
- User & group: nginx:srvadm
- Files: 460 (- r-- rw- ---)
- Directories: 570 (d r-x rwx ---)

Here the user (nginx) could only read file, no writing or execution. It'll be able to read and execute (going into) directories to be able to reach files to serve.

The group (srvadm) can read and execute (going into) the directories to work with files. It'll will be able to read, write and execute directories (going into) to works with file. (opening, creating, modifying, renaming and removing them).

Other won't be able to do anything, files or group.

PS: I do know how theses works mostly, but it really start to be confusing easily between number code and alphabetical sign (rwx), as mixing who needs what and not more, etc. I tried to review my post several times to catch up typo or error, sorry if there is still some.

Regards,
GASPARD DE RENEFORT Kévin
_________________
Traduction wiki, pour praticiper.
Custom logos/biz card/website.


Last edited by kgdrenefort on Thu Jun 13, 2024 12:44 pm; edited 1 time in total
Back to top
View user's profile Send private message
bitwisegamgee
n00b
n00b


Joined: 12 Jul 2023
Posts: 4
Location: Kalamazoo, MI

PostPosted: Wed Jun 12, 2024 1:39 pm    Post subject: Reply with quote

Greetings,

So here are my thoughts:

1. You create a user and set permissions:
Code:

     useradd -M -s /usr/sbin/nologin srvadm
     passwd -l srvadm



2. We now create the root directory, note, I created a directory called /var/www/databaseFE for this


Code:
mkdir -p /var/www/<site>
chown -R srvadm:nginx  /var/www/<site>
find  /var/www/<site> -type d -exec chmod 750 {} \;
find  /var/www/<site> -type f -exec chmod 640 {} \;


3. I view this as optional, but if you want to do a serparate log directory:


Code:
mkdir -p /var/www/<site>/logs
chown -R srvadm:nginx /var/www/<site>/logs
chmod 750 /var/www/<site>/logs



4. Modify your NGINX server to point to this directory.

Re-reading your post, I the way I'd carry this out reflects scenario two. I'm a firm believer in separating concerns, so I'd ideally locate the log file away from the server root directory. For my production webserver, it looks like this:

Code:

/var
-- /logs
-----/website
-- /www
-----/website



Hope this helps


Last edited by bitwisegamgee on Wed Jun 12, 2024 1:45 pm; edited 2 times in total
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23024

PostPosted: Wed Jun 12, 2024 1:40 pm    Post subject: Reply with quote

The owner of a file can change the file's mode, so if nginx owns the files, the case 2 mode of r-- will only present a minor hurdle to something that seeks to change the files. Rogue code running as nginx could chmod +w the files, then write to them anyway. The case 1 approach where nginx is only a group member is safer in this regard. Only the srvadmin user can change the mode, and the active mode prevents nginx from writing to the file without a mode change.

If you want to be cautious, you could run nginx in a mount namespace where the whole hierarchy has been remounted read-only. That will prevent writes even if the permissions are too lax.
Back to top
View user's profile Send private message
kgdrenefort
Guru
Guru


Joined: 19 Sep 2023
Posts: 318
Location: Somewhere in the 77

PostPosted: Wed Jun 12, 2024 2:25 pm    Post subject: Reply with quote

Thanks for the answer.

From your answer, Hu, I'm glad to have asked => I completely forgot this behaviour, which leads me to, for now, consider the first choice, at least only the user in charge of the file would be able to touch them, while nginx will simply read them.

The mount only things could be nice, but maybe later.

Thanks for your answer too bitwisegamgee.

I leave this topic open a bit, maybe someone would point out another great stuff to know.

I'll keep going on my test.

Regards,
GASPARD DE RENEFORT Kévin
_________________
Traduction wiki, pour praticiper.
Custom logos/biz card/website.
Back to top
View user's profile Send private message
kgdrenefort
Guru
Guru


Joined: 19 Sep 2023
Posts: 318
Location: Somewhere in the 77

PostPosted: Wed Jun 12, 2024 3:22 pm    Post subject: Reply with quote

Just realising chown can't be used from srvadm to change the owner if not root. Fat luck :).

I could add sudo and allow this user to run the chown command I need, but maybe you know a better and practical way instead of installing sudo (which I was not needing until now).

This user isn't allowed to get more privileges with su, blocked.

I would love to avoid to run doas, sudo, if it's possible, to keep things simple as it could be. But if there is no other ways, I'll deal with it.

And of course I would like to avoid needing any more permissions (like using root), but at some point, I have to get the good permissions to execute this, simple as that :).

Open for suggestion.

Regards,
GASPARD DE RENEFORT Kévin
_________________
Traduction wiki, pour praticiper.
Custom logos/biz card/website.
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23024

PostPosted: Wed Jun 12, 2024 3:43 pm    Post subject: Reply with quote

Why do you bring up chown? Prior to your most recent post, we were looking at having srvadm own the files, srvadm set the group to nginx, and srvadm set the file mode bits. That should all be fine without root. As you say, chown needs root, but I do not understand why you need chown at all.
Back to top
View user's profile Send private message
kgdrenefort
Guru
Guru


Joined: 19 Sep 2023
Posts: 318
Location: Somewhere in the 77

PostPosted: Thu Jun 13, 2024 8:48 am    Post subject: Reply with quote

Hu wrote:
Why do you bring up chown? Prior to your most recent post, we were looking at having srvadm own the files, srvadm set the group to nginx, and srvadm set the file mode bits. That should all be fine without root. As you say, chown needs root, but I do not understand why you need chown at all.


Sorry, it was a confuse message.

Let me re-explain :

I tried from your answers to apply permissions and ownership (user and group) with the srvadm user. My goal is to avoid in the future to use root privileges to create the document root hierarchy for a new virtual host, because it could avoid error that could be very problematic. Once I tried to write this kind on script for my usage, as today, and I was very stupid and did a chown and chmod… on the root file system. Messy.

To avoid such problem, I want to run all needed command from a very low-privileged user, it's more secure and less risky for the sanity of my system surely.

Usually, I use chown and chmod as root (if not needed, with a simple user for chmod if it's about giving +x to an already owned file for example). I realize of course that chown was needing more privileges, I should have not posted that yesterday and think about it until today :).

So, from your new answer, I could and should:

1/ srvadm can and should set the group to nginx
2/ srvadm set the files and dir mode bits (aka chmod to change permissions)

Point 1/ was my chown attempt. If you seems so surprised to see me trying to use chown, I guess there is two other choices:

a) Using another command, such as chgrp maybe ? To be honnest, in ~20 years I never needed it, maybe it's the day.
b) I really did not understood how I should set owner and group to srvadm:nginx without using a command as chown.

No need to rush an answer, I'm replying because my memory is awful. I'll do some testing and come back if needed and at least to report what and how I did it.

Regards,
GASPARD DE RENEFORT Kévin
_________________
Traduction wiki, pour praticiper.
Custom logos/biz card/website.
Back to top
View user's profile Send private message
kgdrenefort
Guru
Guru


Joined: 19 Sep 2023
Posts: 318
Location: Somewhere in the 77

PostPosted: Thu Jun 13, 2024 9:57 am    Post subject: Reply with quote

After some reading and testing, I'm still stuck.

I tried chgrp and chown to change from srvadm (the non-privileged user) the groups of my directories to nginx, fat luck still.

I saw my srvadm user needs to be in the nginx groups as well, so:

Code:
$ groups srvadm
nginx srvadm


But I get operation non-permitted again.

Every directories has as mode:

Code:
drwxr-x---


And all of them is owned by srvadm:srvadm.

I tried to set 770 for the dir the time to test if I needed writing privileges for groups before proceeding, not better.

Finally, this is the options I use for chown/chgrp:

Code:
--verbose --preserve-root --recursive -P


But resulting in:

Code:
failed to change ownership of '/the/path' from srvadm:srvadm to srvadm:nginx
chown: changing ownership of '/the/path': Operation not permitted


I started to thing the permissions and ownership for the parent directories was incorrect, I set srvadm:nginx for the parent dir (holding the sub-dir for documentroot) without recursivity. Not better sadly.

I might started to be a bit confuse, am I really outside the good path here ?

Thanks as always.

Regards,
GASPARD DE RENEFORT Kévin
_________________
Traduction wiki, pour praticiper.
Custom logos/biz card/website.
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23024

PostPosted: Thu Jun 13, 2024 11:46 am    Post subject: Reply with quote

To use chgrp as an unprivileged user, the target group must be one in which the calling process is a member. You showed groups srvadm, but this looked up the groups that a newly logged in srvadm would normally be issued. Your shell actually using srvadm might have a different group list, particularly if it predates you adding the nginx group to the srvadm user. What is the output of id; chgrp nginx /the/path, all from the srvadm shell that you want to use?

Using chgrp is just a shortcut when you know you will not change the ownership. Calling chown as an unprivileged user can work if you use it only to change group membership, but not the file's owner.

File mode permissions will not lead to EPERM for chgrp, so we can ignore the file mode bits for now.
Back to top
View user's profile Send private message
kgdrenefort
Guru
Guru


Joined: 19 Sep 2023
Posts: 318
Location: Somewhere in the 77

PostPosted: Thu Jun 13, 2024 12:43 pm    Post subject: Reply with quote

Hu wrote:
To use chgrp as an unprivileged user, the target group must be one in which the calling process is a member. You showed groups srvadm, but this looked up the groups that a newly logged in srvadm would normally be issued. Your shell actually using srvadm might have a different group list, particularly if it predates you adding the nginx group to the srvadm user. What is the output of id; chgrp nginx /the/path, all from the srvadm shell that you want to use?

Using chgrp is just a shortcut when you know you will not change the ownership. Calling chown as an unprivileged user can work if you use it only to change group membership, but not the file's owner.

File mode permissions will not lead to EPERM for chgrp, so we can ignore the file mode bits for now.


You smart. I didn't think of trying in a new logged session of srvadm, now it works very well !

For more security, since the file and dir's owners are not supposed to see their user change, only group, I'll use chgrp I think to avoid undesired behaviour. I'm clumsy :).

I think this is really over this time for this matter.

Thanks for your time.

Regards,
GASPARD DE RENEFORT Kévin
_________________
Traduction wiki, pour praticiper.
Custom logos/biz card/website.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum