Re: [RFC] [PATCH 1/6] Kprobes: Allow/deny exclusive write access to inodes

From: Prasanna S Panchamukhi
Date: Tue May 09 2006 - 02:58:48 EST


This patch adds two new wrapper routines to namei.c file
to decrement and increment the inode writecount. Other
routine deny_write_access() decrements the inode
writecount for a given file pointer. But there is no
wrapper routine that decrement's the inode writecount
for a given inode pointer. Also there is no routine that
increment's the inode writecount, if it less than zero.
Even the existing deny_write_access() is modified to use
the new wrapper routine. Kprobe's user-space probes uses
these wrapper routines to get and release exclusive
write access to the probed binary.

Signed-off-by: Prasanna S Panchamukhi <prasanna@xxxxxxxxxx>


fs/namei.c | 34 +++++++++++++++++++++++++++++++---
include/linux/namei.h | 2 ++
2 files changed, 33 insertions(+), 3 deletions(-)

diff -puN fs/namei.c~kprobes_userspace_probes-denywrite-to-inode fs/namei.c
--- linux-2.6.17-rc3-mm1/fs/namei.c~kprobes_userspace_probes-denywrite-to-inode 2006-05-09 10:08:38.000000000 +0530
+++ linux-2.6.17-rc3-mm1-prasanna/fs/namei.c 2006-05-09 10:08:39.000000000 +0530
@@ -322,10 +322,13 @@ int get_write_access(struct inode * inod
return 0;
}

-int deny_write_access(struct file * file)
+/* This routine decrements the writecount for a given inode to
+ * get exclusive write access, so that the file on which probes
+ * are currently applied does not change. User-space probes
+ * uses this routine.
+ */
+int deny_write_access_to_inode(struct inode *inode)
{
- struct inode *inode = file->f_dentry->d_inode;
-
spin_lock(&inode->i_lock);
if (atomic_read(&inode->i_writecount) > 0) {
spin_unlock(&inode->i_lock);
@@ -337,6 +340,31 @@ int deny_write_access(struct file * file
return 0;
}

+/* This routine increments the writecount for a given inode.
+ * to release the write lock. User-space probes uses this
+ * routine.
+ */
+int write_access_to_inode(struct inode *inode)
+{
+ spin_lock(&inode->i_lock);
+ if (atomic_read(&inode->i_writecount) >= 0) {
+ spin_unlock(&inode->i_lock);
+ return -ETXTBSY;
+ }
+ atomic_inc(&inode->i_writecount);
+ spin_unlock(&inode->i_lock);
+
+ return 0;
+}
+
+/* Wrapper routine that decrements the writecount for a given file pointer. */
+int deny_write_access(struct file * file)
+{
+ struct inode *inode = file->f_dentry->d_inode;
+
+ return deny_write_access_to_inode(inode);
+}
+
void path_release(struct nameidata *nd)
{
dput(nd->dentry);
diff -puN include/linux/namei.h~kprobes_userspace_probes-denywrite-to-inode include/linux/namei.h
--- linux-2.6.17-rc3-mm1/include/linux/namei.h~kprobes_userspace_probes-denywrite-to-inode 2006-05-09 10:08:38.000000000 +0530
+++ linux-2.6.17-rc3-mm1-prasanna/include/linux/namei.h 2006-05-09 10:08:39.000000000 +0530
@@ -81,6 +81,8 @@ extern int follow_up(struct vfsmount **,

extern struct dentry *lock_rename(struct dentry *, struct dentry *);
extern void unlock_rename(struct dentry *, struct dentry *);
+extern int deny_write_access_to_inode(struct inode *inode);
+extern int write_access_to_inode(struct inode *inode);

static inline void nd_set_link(struct nameidata *nd, char *path)
{

_
--
Prasanna S Panchamukhi
Linux Technology Center
India Software Labs, IBM Bangalore
Email: prasanna@xxxxxxxxxx
Ph: 91-80-41776329
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/