1. What is SUID and SGID in Linux ?
Unix permissions: The sticky, SUID and SGID bits
Hello, today I'll write about more permissions, this time it will be about the Sticky, SUID and SGID bits.
I'll write about them because one of my readers told me that it would be a good idea to write about these bits.
Sticky bit
What the sticky bit does is that when you execute an application it will be residing in the memory, so if other user (thinking in a multiuser environment) executes that same application it will run faster because it's already active in memory.
So this permission fastens the executions if multiple users are using the same application.
SUID bit
If we apply the SUID bit to an application, it will run with the UID of the owner even if you are logged in as another user. For example if all the users need to execute fdisk without using sudo or escalate to root we just have to apply this bit to fdisk.
SGID bit
The same as SUID but it applies to the group owner of the application.
The SUID bit to execute bash scripts as root
Even if we can apply SGID to execute applications with a different user as the one we are logged in, there are things we can't do even if the owner of the script is root.
First of all, for a script to execute as root we have to apply the SUID bit to the shell we use to execute scripts, because the application in this case is the shell, the script is just a file that will be interpreted by the shell.
Special commandas like adduser can't be executed by any other user even if it has the SUID bit of root. So it won't work a script that executes the adduser command even if the shell has the SUID bit. If you don't believe me tray it ;) I even set the SUID bit for root to my shell, to adduser, to my script that executes adduser and the system didn't permit it.
The SUID bit and GTK+
GTK doesn't support the use of SUID or SGID. So if you try to run a GTK based application with one of these birs the execution will send and alert and it will not start.
Then how can we use these bits?
It can be used to create scripts that write files inside directories in which other users don't have permissions. For example, that any user can create a file inside the root home directory.
Or like I said it before, is possible to use some applications with SUID and SGID like fdisk.
And now you may be asking, well a script to write file in /root, but hadn't you said that to make this work you have to apply the SUID bit to the shell? OK, do this can be risky, to let the shell execute always as root with any user, but there is a trick to make it work without making all your shell root.
We just have to copy our shell to other directory and in that copy apply the SUID bit so the original shell doesn't have to be executed always by root. Then we point our sh to this shell.
Even though this is not a good practice because it isn't secure to allow scripts to be executed as root, but SGID can make easy a lot of things if the server or computer where it is going to be apply is managed only by the right people or SysAdmins.
Unix permissions: The sticky, SUID and SGID bits
Hello, today I'll write about more permissions, this time it will be about the Sticky, SUID and SGID bits.
I'll write about them because one of my readers told me that it would be a good idea to write about these bits.
Sticky bit
What the sticky bit does is that when you execute an application it will be residing in the memory, so if other user (thinking in a multiuser environment) executes that same application it will run faster because it's already active in memory.
So this permission fastens the executions if multiple users are using the same application.
SUID bit
If we apply the SUID bit to an application, it will run with the UID of the owner even if you are logged in as another user. For example if all the users need to execute fdisk without using sudo or escalate to root we just have to apply this bit to fdisk.
SGID bit
The same as SUID but it applies to the group owner of the application.
The SUID bit to execute bash scripts as root
Even if we can apply SGID to execute applications with a different user as the one we are logged in, there are things we can't do even if the owner of the script is root.
First of all, for a script to execute as root we have to apply the SUID bit to the shell we use to execute scripts, because the application in this case is the shell, the script is just a file that will be interpreted by the shell.
Special commandas like adduser can't be executed by any other user even if it has the SUID bit of root. So it won't work a script that executes the adduser command even if the shell has the SUID bit. If you don't believe me tray it ;) I even set the SUID bit for root to my shell, to adduser, to my script that executes adduser and the system didn't permit it.
The SUID bit and GTK+
GTK doesn't support the use of SUID or SGID. So if you try to run a GTK based application with one of these birs the execution will send and alert and it will not start.
Then how can we use these bits?
It can be used to create scripts that write files inside directories in which other users don't have permissions. For example, that any user can create a file inside the root home directory.
Or like I said it before, is possible to use some applications with SUID and SGID like fdisk.
And now you may be asking, well a script to write file in /root, but hadn't you said that to make this work you have to apply the SUID bit to the shell? OK, do this can be risky, to let the shell execute always as root with any user, but there is a trick to make it work without making all your shell root.
We just have to copy our shell to other directory and in that copy apply the SUID bit so the original shell doesn't have to be executed always by root. Then we point our sh to this shell.
Even though this is not a good practice because it isn't secure to allow scripts to be executed as root, but SGID can make easy a lot of things if the server or computer where it is going to be apply is managed only by the right people or SysAdmins.
No comments:
Post a Comment