LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 03-05-2010, 08:41 PM   #1
eboy98
Member
 
Registered: Oct 2007
Distribution: RHEL5
Posts: 64

Rep: Reputation: 15
why newly created file permission always 644...umask issue


Hello There,

i have some confusion about umask.What i have learned by this time.... with umaks value i can control the permission of new files. Like if i set umaks value to 077 for an user then that users newly created file permission would be (777-077) = 700.

I use /etc/bashrc file

Code:
# Even for non-interactive, non-login shells.
if [ $UID -gt 99 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 077
else
    umask 022
fi
In this file i have changed the red color umask value.Now i su to john try to create new file

Code:
[john@pc5 ~]$ touch file
[john@pc5 ~]$ ll
total 1
-rw-r--r--+ 1 john john 0 Mar  5 21:36 file
[john@pc5 ~]$ umask
0077
Here new file create with permission value is 644 ?

I don't get it.How can I make this work?

Thank You.
 
Old 03-05-2010, 11:30 PM   #2
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by eboy98 View Post
Like if i set umaks value to 077 for an user then that users newly created file permission would be (777-077) = 700.
For files (instead of directories), subtract the umask from 666, not 777. For example, a umask of 022 would mean files of 644 (666-022) and directories of 755 (777-022). The 077 umask you're using results in 589 for files, which is an invalid mode. I'm willing to bet that if you create a directory it'll be set to 700, though.

Last edited by win32sux; 03-05-2010 at 11:33 PM.
 
Old 03-06-2010, 09:14 PM   #3
spacewrench
LQ Newbie
 
Registered: Mar 2010
Location: Pacific Northwet
Distribution: Ubuntu, mostly
Posts: 6

Rep: Reputation: 0
That's weird, it works for me (Ubuntu 9.something, bash).

Here's me testing several different umasks:

Code:
dhm@voodoo$ umask
0002
dhm@voodoo$ touch x
dhm@voodoo$ ls -l x
-rw-rw-r-- 1 dhm mersenne 0 2010-03-06 19:07 x
dhm@voodoo$ umask 022
dhm@voodoo$ umask
0022
dhm@voodoo$ touch y
dhm@voodoo$ ls -l y
-rw-r--r-- 1 dhm mersenne 0 2010-03-06 19:07 y
dhm@voodoo$ umask 077
dhm@voodoo$ umask
0077
dhm@voodoo$ touch z
dhm@voodoo$ ls -l
total 0
-rw-rw-r-- 1 dhm mersenne 0 2010-03-06 19:07 x
-rw-r--r-- 1 dhm mersenne 0 2010-03-06 19:07 y
-rw------- 1 dhm mersenne 0 2010-03-06 19:07 z
Incidentally, you don't do the calculation by subtracting, it's by logical NOT-AND. Also, the program that creates the file or directory specifies a protection, which is then NANDed with the process' umask (if I'm remembering everything correctly). So the touch program probably specifies a protection of 0666 (rw-rw-rw) and some bits of that are usually turned off by the umask. Mkdir, on the other hand, probably specifies a protection of 0777 (rwxrwxrwx) and some of those bits are turned off by the umask. A program such as a linker that is creating an executable file will probably specify a default protection of 0777, so that the file will have the X bit set.

HTH
 
Old 03-06-2010, 11:34 PM   #4
eboy98
Member
 
Registered: Oct 2007
Distribution: RHEL5
Posts: 64

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by spacewrench View Post
That's weird, it works for me (Ubuntu 9.something, bash).

HTH
Thanks for for you answer,I know this can be done by using umask command.
but a as a root if i want to set a default umask value for one of my user then how should i proceed.... Is it even possible?
 
Old 03-07-2010, 12:03 AM   #5
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
eboy98, why don't you (as a test) just set the umask in your conditional to something like 026 instead of 077 and then check whether user john's files get created by touch as 640 (666-026) and his directories by mkdir as 751 (777-026).

If they do, then you know your current approach already works.

Last edited by win32sux; 03-07-2010 at 12:05 AM.
 
Old 03-07-2010, 12:25 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
When a new file is created it is created with 666 permissions. The umask value is then ANDed with these permissions to determine what the permissions will be set to.

In your scripts, always precede the mask values with 0 because they are octal numbers.
Inside $((..)) or [[ .. ]], an expression may be interpreted as an integer. If you use 177 instead of 0177, it might not be what you think.

> um=$((0177))
> echo $um
127

> um=$((177))
> printf "%o\n" $um
261
 
1 members found this post helpful.
Old 03-07-2010, 07:55 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You could use the symbolic notation instead; it sets what you ask for
Quote:
If a symbolic mask is specified, the actual file permission bits, and not the inverse, should be specified.
http://linux.die.net/man/1/umask
 
Old 03-09-2010, 03:12 AM   #8
eboy98
Member
 
Registered: Oct 2007
Distribution: RHEL5
Posts: 64

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by win32sux View Post
eboy98, why don't you (as a test) just set the umask in your conditional to something like 026 instead of 077 and then check whether user john's files get created by touch as 640 (666-026) and his directories by mkdir as 751 (777-026).

If they do, then you know your current approach already works.
I dont know what happened...but now every thing works fine....i can change user umask value from /etc/bashrc file....i dont understand what happened earlier....

I am using rhel5 at VirtualBox.


Code:
[john@pc5 ~]$ umask
0026
[john@pc5 ~]$ touch file
[john@pc5 ~]$ ll
total 4
-rw-r----- 1 john john 0 Mar  6 10:58 file
[john@pc5 ~]$ mkdir testDir
[john@pc5 ~]$ ll
total 12
-rw-r----- 1 john john    0 Mar  6 10:58 file
drwxr-x--x 2 john john 4096 Mar  6 10:58 testDir
[john@pc5 ~]$
 
Old 03-09-2010, 03:20 AM   #9
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by eboy98 View Post
I dont know what happened...but now every thing works fine....i can change user umask value from /etc/bashrc file....i dont understand what happened earlier....

I am using rhel5 at VirtualBox.


Code:
[john@pc5 ~]$ umask
0026
[john@pc5 ~]$ touch file
[john@pc5 ~]$ ll
total 4
-rw-r----- 1 john john 0 Mar  6 10:58 file
[john@pc5 ~]$ mkdir testDir
[john@pc5 ~]$ ll
total 12
-rw-r----- 1 john john    0 Mar  6 10:58 file
drwxr-x--x 2 john john 4096 Mar  6 10:58 testDir
[john@pc5 ~]$
Yeah, that pretty much confirms your script is working properly.

What do you get if you run that same exact test with the 077 umask value?
 
Old 03-10-2010, 02:38 AM   #10
eboy98
Member
 
Registered: Oct 2007
Distribution: RHEL5
Posts: 64

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by win32sux View Post
Yeah, that pretty much confirms your script is working properly.

What do you get if you run that same exact test with the 077 umask value?

Yes it also changed.....that you were expecting,mentioned at the earlier post.

Here is the output:


Code:
[john@pc5 ~]$ umask
0077
[john@pc5 ~]$ touch file
[john@pc5 ~]$ mkdir testDir
[john@pc5 ~]$ ll
total 12
-rw------- 1 john john    0 Mar  6 12:42 file
drwx------ 2 john john 4096 Mar  6 12:42 testDir
[john@pc5 ~]$

Last edited by eboy98; 03-10-2010 at 02:40 AM. Reason: forgate to add umask value on code
 
Old 03-10-2010, 03:38 AM   #11
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by eboy98 View Post
Yes it also changed.....that you were expecting,mentioned at the earlier post.

Here is the output:


Code:
[john@pc5 ~]$ umask
0077
[john@pc5 ~]$ touch file
[john@pc5 ~]$ mkdir testDir
[john@pc5 ~]$ ll
total 12
-rw------- 1 john john    0 Mar  6 12:42 file
drwx------ 2 john john 4096 Mar  6 12:42 testDir
[john@pc5 ~]$
Okay, so the directory gets set to 700 (as expected), but the file gets set to 600. This matches the behavior which spacewrench observed when using the 077 umask value. So it still looks (to me, at least) like 589, being an invalid mode, triggers the use of a fallback mode (which would explain why the other umask value you tried works just fine). Could anyone confirm whether that is indeed what is happening? If so, where is the fallback mode obtained from?
 
Old 03-13-2010, 04:27 AM   #12
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If you enter "umask 589", the argument is rejected and the umask isn't changed. The argument must be an octal number and be a legal value.
I just use bash, and umask is a shell builtin function.
 
Old 03-13-2010, 04:37 AM   #13
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by jschiwal View Post
If you enter "umask 589", the argument is rejected and the umask isn't changed.
Wait, I'm not sure I'm following you — why'd you try 589 as a umask value?
 
Old 03-13-2010, 04:50 AM   #14
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I wouldn't.
Quote:
So it still looks (to me, at least) like 589, being an invalid mode, triggers the use of a fallback mode.
 
Old 03-13-2010, 04:57 AM   #15
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
What I was referring to in that quote was that 589 would be the resulting file permission mode for a 077 umask value. Since we know a file can't have 589 permissions, a different mode for files is being used when a umask 077 value is specified. Based on the above posts, that mode is 600, but how (or from where) is that value automatically computed (or selected)?

Last edited by win32sux; 03-13-2010 at 04:59 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cp: cannot stat: Permission denied, even though file is 644? grayFalcon Linux - Newbie 3 10-05-2009 02:40 AM
Group write access for newly created files/directories without changing umask fhd Linux - Security 3 04-05-2009 05:28 AM
Is there any other way of finding out if my file is just modified or is newly created deepti Linux - General 3 01-07-2009 10:35 PM
FTP, newly created files permission georgiozoze Linux - Newbie 2 09-16-2006 08:47 PM
Make newly created file executable Black Chaos Linux - General 3 08-03-2006 10:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 02:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration