2005-04-16 18:20:36 -04:00
|
|
|
/*
|
|
|
|
* linux/fs/attr.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
|
|
|
* changes by Thomas Schoebel-Theuer
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/time.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/string.h>
|
2006-01-11 15:17:46 -05:00
|
|
|
#include <linux/capability.h>
|
[PATCH] inotify
inotify is intended to correct the deficiencies of dnotify, particularly
its inability to scale and its terrible user interface:
* dnotify requires the opening of one fd per each directory
that you intend to watch. This quickly results in too many
open files and pins removable media, preventing unmount.
* dnotify is directory-based. You only learn about changes to
directories. Sure, a change to a file in a directory affects
the directory, but you are then forced to keep a cache of
stat structures.
* dnotify's interface to user-space is awful. Signals?
inotify provides a more usable, simple, powerful solution to file change
notification:
* inotify's interface is a system call that returns a fd, not SIGIO.
You get a single fd, which is select()-able.
* inotify has an event that says "the filesystem that the item
you were watching is on was unmounted."
* inotify can watch directories or files.
Inotify is currently used by Beagle (a desktop search infrastructure),
Gamin (a FAM replacement), and other projects.
See Documentation/filesystems/inotify.txt.
Signed-off-by: Robert Love <rml@novell.com>
Cc: John McCutchan <ttb@tentacle.dhs.org>
Cc: Christoph Hellwig <hch@lst.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-07-12 17:06:03 -04:00
|
|
|
#include <linux/fsnotify.h>
|
2005-04-16 18:20:36 -04:00
|
|
|
#include <linux/fcntl.h>
|
|
|
|
#include <linux/quotaops.h>
|
|
|
|
#include <linux/security.h>
|
|
|
|
|
|
|
|
/* Taken over from the old code... */
|
|
|
|
|
|
|
|
/* POSIX UID/GID verification for setting inode attributes. */
|
|
|
|
int inode_change_ok(struct inode *inode, struct iattr *attr)
|
|
|
|
{
|
|
|
|
int retval = -EPERM;
|
|
|
|
unsigned int ia_valid = attr->ia_valid;
|
|
|
|
|
|
|
|
/* If force is set do it anyway. */
|
|
|
|
if (ia_valid & ATTR_FORCE)
|
|
|
|
goto fine;
|
|
|
|
|
|
|
|
/* Make sure a caller can chown. */
|
|
|
|
if ((ia_valid & ATTR_UID) &&
|
|
|
|
(current->fsuid != inode->i_uid ||
|
|
|
|
attr->ia_uid != inode->i_uid) && !capable(CAP_CHOWN))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* Make sure caller can chgrp. */
|
|
|
|
if ((ia_valid & ATTR_GID) &&
|
|
|
|
(current->fsuid != inode->i_uid ||
|
|
|
|
(!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid)) &&
|
|
|
|
!capable(CAP_CHOWN))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* Make sure a caller can chmod. */
|
|
|
|
if (ia_valid & ATTR_MODE) {
|
2007-07-17 05:30:08 -04:00
|
|
|
if (!is_owner_or_cap(inode))
|
2005-04-16 18:20:36 -04:00
|
|
|
goto error;
|
|
|
|
/* Also check the setgid bit! */
|
|
|
|
if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
|
|
|
|
inode->i_gid) && !capable(CAP_FSETID))
|
|
|
|
attr->ia_mode &= ~S_ISGID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for setting the inode time. */
|
|
|
|
if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
|
2007-07-17 05:30:08 -04:00
|
|
|
if (!is_owner_or_cap(inode))
|
2005-04-16 18:20:36 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
fine:
|
|
|
|
retval = 0;
|
|
|
|
error:
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(inode_change_ok);
|
|
|
|
|
|
|
|
int inode_setattr(struct inode * inode, struct iattr * attr)
|
|
|
|
{
|
|
|
|
unsigned int ia_valid = attr->ia_valid;
|
2006-01-08 04:02:39 -05:00
|
|
|
|
|
|
|
if (ia_valid & ATTR_SIZE &&
|
|
|
|
attr->ia_size != i_size_read(inode)) {
|
|
|
|
int error = vmtruncate(inode, attr->ia_size);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ia_valid & ATTR_UID)
|
|
|
|
inode->i_uid = attr->ia_uid;
|
|
|
|
if (ia_valid & ATTR_GID)
|
|
|
|
inode->i_gid = attr->ia_gid;
|
|
|
|
if (ia_valid & ATTR_ATIME)
|
|
|
|
inode->i_atime = timespec_trunc(attr->ia_atime,
|
|
|
|
inode->i_sb->s_time_gran);
|
|
|
|
if (ia_valid & ATTR_MTIME)
|
|
|
|
inode->i_mtime = timespec_trunc(attr->ia_mtime,
|
|
|
|
inode->i_sb->s_time_gran);
|
|
|
|
if (ia_valid & ATTR_CTIME)
|
|
|
|
inode->i_ctime = timespec_trunc(attr->ia_ctime,
|
|
|
|
inode->i_sb->s_time_gran);
|
|
|
|
if (ia_valid & ATTR_MODE) {
|
|
|
|
umode_t mode = attr->ia_mode;
|
|
|
|
|
|
|
|
if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
|
|
|
|
mode &= ~S_ISGID;
|
|
|
|
inode->i_mode = mode;
|
|
|
|
}
|
|
|
|
mark_inode_dirty(inode);
|
2006-01-08 04:02:39 -05:00
|
|
|
|
|
|
|
return 0;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(inode_setattr);
|
|
|
|
|
|
|
|
int notify_change(struct dentry * dentry, struct iattr * attr)
|
|
|
|
{
|
|
|
|
struct inode *inode = dentry->d_inode;
|
|
|
|
mode_t mode;
|
|
|
|
int error;
|
|
|
|
struct timespec now;
|
|
|
|
unsigned int ia_valid = attr->ia_valid;
|
|
|
|
|
|
|
|
mode = inode->i_mode;
|
|
|
|
now = current_fs_time(inode->i_sb);
|
|
|
|
|
|
|
|
attr->ia_ctime = now;
|
|
|
|
if (!(ia_valid & ATTR_ATIME_SET))
|
|
|
|
attr->ia_atime = now;
|
|
|
|
if (!(ia_valid & ATTR_MTIME_SET))
|
|
|
|
attr->ia_mtime = now;
|
Implement file posix capabilities
Implement file posix capabilities. This allows programs to be given a
subset of root's powers regardless of who runs them, without having to use
setuid and giving the binary all of root's powers.
This version works with Kaigai Kohei's userspace tools, found at
http://www.kaigai.gr.jp/index.php. For more information on how to use this
patch, Chris Friedhoff has posted a nice page at
http://www.friedhoff.org/fscaps.html.
Changelog:
Nov 27:
Incorporate fixes from Andrew Morton
(security-introduce-file-caps-tweaks and
security-introduce-file-caps-warning-fix)
Fix Kconfig dependency.
Fix change signaling behavior when file caps are not compiled in.
Nov 13:
Integrate comments from Alexey: Remove CONFIG_ ifdef from
capability.h, and use %zd for printing a size_t.
Nov 13:
Fix endianness warnings by sparse as suggested by Alexey
Dobriyan.
Nov 09:
Address warnings of unused variables at cap_bprm_set_security
when file capabilities are disabled, and simultaneously clean
up the code a little, by pulling the new code into a helper
function.
Nov 08:
For pointers to required userspace tools and how to use
them, see http://www.friedhoff.org/fscaps.html.
Nov 07:
Fix the calculation of the highest bit checked in
check_cap_sanity().
Nov 07:
Allow file caps to be enabled without CONFIG_SECURITY, since
capabilities are the default.
Hook cap_task_setscheduler when !CONFIG_SECURITY.
Move capable(TASK_KILL) to end of cap_task_kill to reduce
audit messages.
Nov 05:
Add secondary calls in selinux/hooks.c to task_setioprio and
task_setscheduler so that selinux and capabilities with file
cap support can be stacked.
Sep 05:
As Seth Arnold points out, uid checks are out of place
for capability code.
Sep 01:
Define task_setscheduler, task_setioprio, cap_task_kill, and
task_setnice to make sure a user cannot affect a process in which
they called a program with some fscaps.
One remaining question is the note under task_setscheduler: are we
ok with CAP_SYS_NICE being sufficient to confine a process to a
cpuset?
It is a semantic change, as without fsccaps, attach_task doesn't
allow CAP_SYS_NICE to override the uid equivalence check. But since
it uses security_task_setscheduler, which elsewhere is used where
CAP_SYS_NICE can be used to override the uid equivalence check,
fixing it might be tough.
task_setscheduler
note: this also controls cpuset:attach_task. Are we ok with
CAP_SYS_NICE being used to confine to a cpuset?
task_setioprio
task_setnice
sys_setpriority uses this (through set_one_prio) for another
process. Need same checks as setrlimit
Aug 21:
Updated secureexec implementation to reflect the fact that
euid and uid might be the same and nonzero, but the process
might still have elevated caps.
Aug 15:
Handle endianness of xattrs.
Enforce capability version match between kernel and disk.
Enforce that no bits beyond the known max capability are
set, else return -EPERM.
With this extra processing, it may be worth reconsidering
doing all the work at bprm_set_security rather than
d_instantiate.
Aug 10:
Always call getxattr at bprm_set_security, rather than
caching it at d_instantiate.
[morgan@kernel.org: file-caps clean up for linux/capability.h]
[bunk@kernel.org: unexport cap_inode_killpriv]
Signed-off-by: Serge E. Hallyn <serue@us.ibm.com>
Cc: Stephen Smalley <sds@tycho.nsa.gov>
Cc: James Morris <jmorris@namei.org>
Cc: Chris Wright <chrisw@sous-sol.org>
Cc: Andrew Morgan <morgan@kernel.org>
Signed-off-by: Andrew Morgan <morgan@kernel.org>
Signed-off-by: Adrian Bunk <bunk@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-10-17 02:31:36 -04:00
|
|
|
if (ia_valid & ATTR_KILL_PRIV) {
|
|
|
|
attr->ia_valid &= ~ATTR_KILL_PRIV;
|
|
|
|
ia_valid &= ~ATTR_KILL_PRIV;
|
|
|
|
error = security_inode_need_killpriv(dentry);
|
|
|
|
if (error > 0)
|
|
|
|
error = security_inode_killpriv(dentry);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
}
|
2005-04-16 18:20:36 -04:00
|
|
|
if (ia_valid & ATTR_KILL_SUID) {
|
|
|
|
attr->ia_valid &= ~ATTR_KILL_SUID;
|
|
|
|
if (mode & S_ISUID) {
|
|
|
|
if (!(ia_valid & ATTR_MODE)) {
|
|
|
|
ia_valid = attr->ia_valid |= ATTR_MODE;
|
|
|
|
attr->ia_mode = inode->i_mode;
|
|
|
|
}
|
|
|
|
attr->ia_mode &= ~S_ISUID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ia_valid & ATTR_KILL_SGID) {
|
|
|
|
attr->ia_valid &= ~ ATTR_KILL_SGID;
|
|
|
|
if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
|
|
|
|
if (!(ia_valid & ATTR_MODE)) {
|
|
|
|
ia_valid = attr->ia_valid |= ATTR_MODE;
|
|
|
|
attr->ia_mode = inode->i_mode;
|
|
|
|
}
|
|
|
|
attr->ia_mode &= ~S_ISGID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!attr->ia_valid)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (ia_valid & ATTR_SIZE)
|
|
|
|
down_write(&dentry->d_inode->i_alloc_sem);
|
|
|
|
|
|
|
|
if (inode->i_op && inode->i_op->setattr) {
|
|
|
|
error = security_inode_setattr(dentry, attr);
|
|
|
|
if (!error)
|
|
|
|
error = inode->i_op->setattr(dentry, attr);
|
|
|
|
} else {
|
|
|
|
error = inode_change_ok(inode, attr);
|
|
|
|
if (!error)
|
|
|
|
error = security_inode_setattr(dentry, attr);
|
|
|
|
if (!error) {
|
|
|
|
if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
|
|
|
|
(ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid))
|
|
|
|
error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0;
|
|
|
|
if (!error)
|
|
|
|
error = inode_setattr(inode, attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ia_valid & ATTR_SIZE)
|
|
|
|
up_write(&dentry->d_inode->i_alloc_sem);
|
|
|
|
|
[PATCH] inotify
inotify is intended to correct the deficiencies of dnotify, particularly
its inability to scale and its terrible user interface:
* dnotify requires the opening of one fd per each directory
that you intend to watch. This quickly results in too many
open files and pins removable media, preventing unmount.
* dnotify is directory-based. You only learn about changes to
directories. Sure, a change to a file in a directory affects
the directory, but you are then forced to keep a cache of
stat structures.
* dnotify's interface to user-space is awful. Signals?
inotify provides a more usable, simple, powerful solution to file change
notification:
* inotify's interface is a system call that returns a fd, not SIGIO.
You get a single fd, which is select()-able.
* inotify has an event that says "the filesystem that the item
you were watching is on was unmounted."
* inotify can watch directories or files.
Inotify is currently used by Beagle (a desktop search infrastructure),
Gamin (a FAM replacement), and other projects.
See Documentation/filesystems/inotify.txt.
Signed-off-by: Robert Love <rml@novell.com>
Cc: John McCutchan <ttb@tentacle.dhs.org>
Cc: Christoph Hellwig <hch@lst.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-07-12 17:06:03 -04:00
|
|
|
if (!error)
|
|
|
|
fsnotify_change(dentry, ia_valid);
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(notify_change);
|