Skip to contentSkip to conversation

Directory link count wrapping on Linux/XFS/i386?

PATCH David Malone 4 participants 5 messages Mar 30, 2005 – Mar 31, 2005 Kbuild

lore
Share & tools
lore
#0 lore raw

I was looking around to see how Linux handles directories with a high link count (ie. when they have many subdirectories) and I think I have stumbled across a bug in the Linux xfs glue.

It seems that internally xfs uses a 32 bit field for the link count, and the stat64 syscalls use a 32 bit field. These fields are copied via the vattr structure in xfs_vnode.h, which uses a nlink_t for the link count. However, in the kernel, I think this field is actually of type __kernel_nlink_t which seems to be 16 bits on many platforms.

I've tested this on an i386 2.6.11 kernel and it seems that the link count presented to userland wraps after 65536 subdirectories. This naturally doesn't let you screw up the filesystem or anything, but it does let you can hide files from find/fts, as demonstrated below.

I guess to fix it you'd change the type of nlink in struct vattr so that it is the same type (unsigned int) as the type in struct kstat. I've included the obvious patch, but I don't have a machine that I can test it on right now.

David.

turing 2% mkdir testdir
turing 3% cd testdir
turing 4% ls -ld .
drwxr-xr-x 2 dwmalone dwmalone 6 Mar 30 12:18 . turing 5% perl ../mk65536dirs.pl
turing 6% ls -ld .
drwxr-xr-x 2 dwmalone dwmalone 1056768 Mar 30 12:19 . turing 7% mkdir .hidden
turing 8% touch .hidden/secret
turing 9% find . -name secret -print

tmp/xfs_vnode.h renamed from usr/src/linux-2.6.11/fs/xfs/linux-2.6/xfs_vnode.h +1 -1
@@ -409,7 +409,7 @@
 	int		va_mask;	/* bit-mask of attributes present */
 	enum vtype	va_type;	/* vnode type (for create) */
 	mode_t		va_mode;	/* file access mode and type */
-	nlink_t		va_nlink;	/* number of references to file */
+	unsigned int	va_nlink;	/* number of references to file */
 	uid_t		va_uid;		/* owner user id */
 	gid_t		va_gid;		/* owner group id */
 	xfs_ino_t	va_nodeid;	/* file id */
#1 lore raw
Quoted 7 lines · David Malone

It seems that internally xfs uses a 32 bit field for the link count, and the stat64 syscalls use a 32 bit field. These fields are copied via the vattr structure in xfs_vnode.h, which uses a nlink_t for the link count. However, in the kernel, I think this field is actually of type __kernel_nlink_t which seems to be 16 bits on many platforms.

Quoted 5 lines

I've tested this on an i386 2.6.11 kernel and it seems that the link count presented to userland wraps after 65536 subdirectories. This naturally doesn't let you screw up the filesystem or anything, but it does let you can hide files from find/fts, as demonstrated below.

The correct fix, used for reiserfs (and a patch for ext3 also) is to set i_nlink = 1 in case the filesystem count has wrapped. When nlink==1 the fts/find code no longer optimizes subdirectory traversal and checks each entries filetype to see if it should recurse.

Cheers, Andreas
--
Andreas Dilger
Principal Software Engineer
Cluster File Systems, Inc.

#2 lore raw
Quoted 7 lines · Andreas Dilger
Quoted 6 lines · David Malone

It seems that internally xfs uses a 32 bit field for the link count, and the stat64 syscalls use a 32 bit field. These fields are copied via the vattr structure in xfs_vnode.h, which uses a nlink_t for the link count. However, in the kernel, I think this field is actually of type __kernel_nlink_t which seems to be 16 bits on many platforms.

Yes, use of nlink_t looks wrong there, thanks. Theres one/two other uses of it in XFS as well, I'll audit those.

Quoted 4 lines

The correct fix, used for reiserfs (and a patch for ext3 also) is to set i_nlink = 1 in case the filesystem count has wrapped. When nlink==1 the fts/find code no longer optimizes subdirectory traversal and checks each entries filetype to see if it should recurse.

Ah, I see - the INC_DIR_INODE_NLINK/DEC_DIR_INODE_NLINK macros, right. I'll look into that too, thanks.

cheers.

Signature · 1 line
Nathan
#3 lore raw
Quoted 8 lines · Nathan Scott
Quoted 4 lines · Andreas Dilger

The correct fix, used for reiserfs (and a patch for ext3 also) is to set i_nlink = 1 in case the filesystem count has wrapped. When nlink==1 the fts/find code no longer optimizes subdirectory traversal and checks each entries filetype to see if it should recurse.

Ah, I see - the INC_DIR_INODE_NLINK/DEC_DIR_INODE_NLINK macros, right. I'll look into that too, thanks.

Hmm, since struct inode has an unsigned int as nlink, it'd seem doing this sort of thing is only useful for filesystems where the ondisk nlink is a 16 bit value (and ext2/3/reiserfs do seem to be in that category, afaict).

So, Davids patch (and those one/two other cases) would seem to be enough to get this resolved. There isn't much using nlink_t - looks like mainly the non-stat64 'stat' calls and one or two other (possibly accidental?) uses like we had in XFS.

cheers.

Signature · 1 line
Nathan
#4 lore raw
In reply to David Malone
Quoted 3 lines

but it does let you can hide files from find/fts, as demonstrated below.

That's because find optimizes its searching by looking at the link count. IIRC, the -noleaf option should make it visible again.

Quoted 3 lines

turing 7% mkdir .hidden
turing 8% touch .hidden/secret
turing 9% find . -name secret -print

Jan Engelhardt

Signature · 1 line
No TOFU for me, please.