Re: How to check if a file is a present in a directory(directly/indirectly)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 06 Mar 2015 20:38:46 +0530, noyb noybee said:
> I am building a LKM in which I need to check whether a file is inside
> a particular directory(directly or inside sub-directories). Is there a
> function for this? Do I need to use the lookup function in
> inode_operations for this or is there a better method?

Inside subdirectories is an intractable problem, even from userspace.  You
basically need to walk *all* the inodes:

find /mounpoint -inum file_inode_number | grep /mountpoint/directory/to/check

If the filesystem is large, that can take a while.

Also, a file can be in more than one subdirectory at once:

[~] cd /tmp
[/tmp] mkdir z9
[/tmp] cd z9
[/tmp/z9] mkdir a b
[/tmp/z9] mkdir a/c
[/tmp/z9] touch a/c/d
[/tmp/z9] ln a/c/d a/first
[/tmp/z9] ln a/c/d b/second
[/tmp/z9] ln a/c/d third
[/tmp/z9] ls -lR
/tmp/z9] ls -liR
.:                              
total 0                         
62326 drwxr-xr-x. 3 valdis valdis 80 Mar  6 12:13 a
62327 drwxr-xr-x. 2 valdis valdis 60 Mar  6 12:13 b
62388 -rw-r--r--. 4 valdis valdis  0 Mar  6 12:13 third
                                
./a:                            
total 0                         
63471 drwxr-xr-x. 2 valdis valdis 60 Mar  6 12:13 c
62388 -rw-r--r--. 4 valdis valdis  0 Mar  6 12:13 first
                                
./a/c:
total 0
62388 -rw-r--r--. 4 valdis valdis 0 Mar  6 12:13 d

./b:
total 0
62388 -rw-r--r--. 4 valdis valdis 0 Mar  6 12:13 second

So which directory is it "under"?  And can this sort of stunt be
exploited with a race condition on a large filesystem? (Hint -there's
a *reason* that SELinux and Smack both use file labels instead of
pathnames...)

I think you need to stop and think about what you're *actually*
trying to accomplish here.  I suspect that what you *think* you are
checking isn't what you should *actually* be checking....

Attachment: pgpi6HDcqMYRO.pgp
Description: PGP signature

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux