Skip to content

Commit 3e42979

Browse files
rwmjonestorvalds
authored andcommitted
procfs: expose umask in /proc/<PID>/status
It's not possible to read the process umask without also modifying it, which is what umask(2) does. A library cannot read umask safely, especially if the main program might be multithreaded. Add a new status line ("Umask") in /proc/<PID>/status. It contains the file mode creation mask (umask) in octal. It is only shown for tasks which have task->fs. This patch is adapted from one originally written by Pierre Carrier. The use case is that we have endless trouble with people setting weird umask() values (usually on the grounds of "security"), and then everything breaking. I'm on the hook to fix these. We'd like to add debugging to our program so we can dump out the umask in debug reports. Previous versions of the patch used a syscall so you could only read your own umask. That's all I need. However there was quite a lot of push-back from those, so this new version exports it in /proc. See: https://lkml.org/lkml/2016/4/13/704 [umask2] https://lkml.org/lkml/2016/4/13/487 [getumask] Signed-off-by: Richard W.M. Jones <[email protected]> Acked-by: Konstantin Khlebnikov <[email protected]> Acked-by: Jerome Marchand <[email protected]> Acked-by: Kees Cook <[email protected]> Cc: "Theodore Ts'o" <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Pierre Carrier <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 623e47f commit 3e42979

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Documentation/filesystems/proc.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ Table 1-2: Contents of the status files (as of 4.1)
225225
TracerPid PID of process tracing this process (0 if not)
226226
Uid Real, effective, saved set, and file system UIDs
227227
Gid Real, effective, saved set, and file system GIDs
228+
Umask file mode creation mask
228229
FDSize number of file descriptor slots currently allocated
229230
Groups supplementary group list
230231
NStgid descendant namespace thread group ID hierarchy

fs/proc/array.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#include <linux/tracehook.h>
8484
#include <linux/string_helpers.h>
8585
#include <linux/user_namespace.h>
86+
#include <linux/fs_struct.h>
8687

8788
#include <asm/pgtable.h>
8889
#include <asm/processor.h>
@@ -139,12 +140,25 @@ static inline const char *get_task_state(struct task_struct *tsk)
139140
return task_state_array[fls(state)];
140141
}
141142

143+
static inline int get_task_umask(struct task_struct *tsk)
144+
{
145+
struct fs_struct *fs;
146+
int umask = -ENOENT;
147+
148+
task_lock(tsk);
149+
fs = tsk->fs;
150+
if (fs)
151+
umask = fs->umask;
152+
task_unlock(tsk);
153+
return umask;
154+
}
155+
142156
static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
143157
struct pid *pid, struct task_struct *p)
144158
{
145159
struct user_namespace *user_ns = seq_user_ns(m);
146160
struct group_info *group_info;
147-
int g;
161+
int g, umask;
148162
struct task_struct *tracer;
149163
const struct cred *cred;
150164
pid_t ppid, tpid = 0, tgid, ngid;
@@ -162,6 +176,10 @@ static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
162176
ngid = task_numa_group_id(p);
163177
cred = get_task_cred(p);
164178

179+
umask = get_task_umask(p);
180+
if (umask >= 0)
181+
seq_printf(m, "Umask:\t%#04o\n", umask);
182+
165183
task_lock(p);
166184
if (p->files)
167185
max_fds = files_fdtable(p->files)->max_fds;

0 commit comments

Comments
 (0)