Skip to content

Commit afefdbb

Browse files
dhowellsLinus Torvalds
authored andcommitted
[PATCH] VFS: Make filldir_t and struct kstat deal in 64-bit inode numbers
These patches make the kernel pass 64-bit inode numbers internally when communicating to userspace, even on a 32-bit system. They are required because some filesystems have intrinsic 64-bit inode numbers: NFS3+ and XFS for example. The 64-bit inode numbers are then propagated to userspace automatically where the arch supports it. Problems have been seen with userspace (eg: ld.so) using the 64-bit inode number returned by stat64() or getdents64() to differentiate files, and failing because the 64-bit inode number space was compressed to 32-bits, and so overlaps occur. This patch: Make filldir_t take a 64-bit inode number and struct kstat carry a 64-bit inode number so that 64-bit inode numbers can be passed back to userspace. The stat functions then returns the full 64-bit inode number where available and where possible. If it is not possible to represent the inode number supplied by the filesystem in the field provided by userspace, then error EOVERFLOW will be issued. Similarly, the getdents/readdir functions now pass the full 64-bit inode number to userspace where possible, returning EOVERFLOW instead when a directory entry is encountered that can't be properly represented. Note that this means that some inodes will not be stat'able on a 32-bit system with old libraries where they were before - but it does mean that there will be no ambiguity over what a 32-bit inode number refers to. Note similarly that directory scans may be cut short with an error on a 32-bit system with old libraries where the scan would work before for the same reasons. It is judged unlikely that this situation will occur because modern glibc uses 64-bit capable versions of stat and getdents class functions exclusively, and that older systems are unlikely to encounter unrepresentable inode numbers anyway. [akpm: alpha build fix] Signed-off-by: David Howells <dhowells@redhat.com> Cc: Trond Myklebust <trond.myklebust@fys.uio.no> Cc: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 1d32849 commit afefdbb

23 files changed

Lines changed: 152 additions & 45 deletions

File tree

‎arch/alpha/kernel/osf_sys.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,26 @@ struct osf_dirent_callback {
111111

112112
static int
113113
osf_filldir(void *__buf, const char *name, int namlen, loff_t offset,
114-
ino_t ino, unsigned int d_type)
114+
u64 ino, unsigned int d_type)
115115
{
116116
struct osf_dirent __user *dirent;
117117
struct osf_dirent_callback *buf = (struct osf_dirent_callback *) __buf;
118118
unsigned int reclen = ROUND_UP(NAME_OFFSET + namlen + 1);
119+
unsigned int d_ino;
119120

120121
buf->error = -EINVAL; /* only used if we fail */
121122
if (reclen > buf->count)
122123
return -EINVAL;
124+
d_ino = ino;
125+
if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
126+
return -EOVERFLOW;
123127
if (buf->basep) {
124128
if (put_user(offset, buf->basep))
125129
return -EFAULT;
126130
buf->basep = NULL;
127131
}
128132
dirent = buf->dirent;
129-
put_user(ino, &dirent->d_ino);
133+
put_user(d_ino, &dirent->d_ino);
130134
put_user(namlen, &dirent->d_namlen);
131135
put_user(reclen, &dirent->d_reclen);
132136
if (copy_to_user(dirent->d_name, name, namlen) ||

‎arch/ia64/ia32/sys_ia32.c‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,23 @@ sys32_execve (char __user *name, compat_uptr_t __user *argv, compat_uptr_t __use
125125

126126
int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
127127
{
128+
compat_ino_t ino;
128129
int err;
129130

130131
if ((u64) stat->size > MAX_NON_LFS ||
131132
!old_valid_dev(stat->dev) ||
132133
!old_valid_dev(stat->rdev))
133134
return -EOVERFLOW;
134135

136+
ino = stat->ino;
137+
if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
138+
return -EOVERFLOW;
139+
135140
if (clear_user(ubuf, sizeof(*ubuf)))
136141
return -EFAULT;
137142

138143
err = __put_user(old_encode_dev(stat->dev), &ubuf->st_dev);
139-
err |= __put_user(stat->ino, &ubuf->st_ino);
144+
err |= __put_user(ino, &ubuf->st_ino);
140145
err |= __put_user(stat->mode, &ubuf->st_mode);
141146
err |= __put_user(stat->nlink, &ubuf->st_nlink);
142147
err |= __put_user(high2lowuid(stat->uid), &ubuf->st_uid);
@@ -1222,24 +1227,28 @@ struct readdir32_callback {
12221227
};
12231228

12241229
static int
1225-
filldir32 (void *__buf, const char *name, int namlen, loff_t offset, ino_t ino,
1230+
filldir32 (void *__buf, const char *name, int namlen, loff_t offset, u64 ino,
12261231
unsigned int d_type)
12271232
{
12281233
struct compat_dirent __user * dirent;
12291234
struct getdents32_callback * buf = (struct getdents32_callback *) __buf;
12301235
int reclen = ROUND_UP(offsetof(struct compat_dirent, d_name) + namlen + 1, 4);
1236+
u32 d_ino;
12311237

12321238
buf->error = -EINVAL; /* only used if we fail.. */
12331239
if (reclen > buf->count)
12341240
return -EINVAL;
1241+
d_ino = ino;
1242+
if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1243+
return -EOVERFLOW;
12351244
buf->error = -EFAULT; /* only used if we fail.. */
12361245
dirent = buf->previous;
12371246
if (dirent)
12381247
if (put_user(offset, &dirent->d_off))
12391248
return -EFAULT;
12401249
dirent = buf->current_dir;
12411250
buf->previous = dirent;
1242-
if (put_user(ino, &dirent->d_ino)
1251+
if (put_user(d_ino, &dirent->d_ino)
12431252
|| put_user(reclen, &dirent->d_reclen)
12441253
|| copy_to_user(dirent->d_name, name, namlen)
12451254
|| put_user(0, dirent->d_name + namlen))
@@ -1287,17 +1296,21 @@ sys32_getdents (unsigned int fd, struct compat_dirent __user *dirent, unsigned i
12871296
}
12881297

12891298
static int
1290-
fillonedir32 (void * __buf, const char * name, int namlen, loff_t offset, ino_t ino,
1299+
fillonedir32 (void * __buf, const char * name, int namlen, loff_t offset, u64 ino,
12911300
unsigned int d_type)
12921301
{
12931302
struct readdir32_callback * buf = (struct readdir32_callback *) __buf;
12941303
struct old_linux32_dirent __user * dirent;
1304+
u32 d_ino;
12951305

12961306
if (buf->count)
12971307
return -EINVAL;
1308+
d_ino = ino;
1309+
if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1310+
return -EOVERFLOW;
12981311
buf->count++;
12991312
dirent = buf->dirent;
1300-
if (put_user(ino, &dirent->d_ino)
1313+
if (put_user(d_ino, &dirent->d_ino)
13011314
|| put_user(offset, &dirent->d_offset)
13021315
|| put_user(namlen, &dirent->d_namlen)
13031316
|| copy_to_user(dirent->d_name, name, namlen)

‎arch/mips/kernel/linux32.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
7777
memset(&tmp, 0, sizeof(tmp));
7878
tmp.st_dev = new_encode_dev(stat->dev);
7979
tmp.st_ino = stat->ino;
80+
if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
81+
return -EOVERFLOW;
8082
tmp.st_mode = stat->mode;
8183
tmp.st_nlink = stat->nlink;
8284
SET_UID(tmp.st_uid, stat->uid);

‎arch/mips/kernel/sysirix.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,12 +1739,13 @@ struct irix_dirent32_callback {
17391739
#define ROUND_UP32(x) (((x)+sizeof(u32)-1) & ~(sizeof(u32)-1))
17401740

17411741
static int irix_filldir32(void *__buf, const char *name,
1742-
int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1742+
int namlen, loff_t offset, u64 ino, unsigned int d_type)
17431743
{
17441744
struct irix_dirent32 __user *dirent;
17451745
struct irix_dirent32_callback *buf = __buf;
17461746
unsigned short reclen = ROUND_UP32(NAME_OFFSET32(dirent) + namlen + 1);
17471747
int err = 0;
1748+
u32 d_ino;
17481749

17491750
#ifdef DEBUG_GETDENTS
17501751
printk("\nirix_filldir32[reclen<%d>namlen<%d>count<%d>]",
@@ -1753,12 +1754,15 @@ static int irix_filldir32(void *__buf, const char *name,
17531754
buf->error = -EINVAL; /* only used if we fail.. */
17541755
if (reclen > buf->count)
17551756
return -EINVAL;
1757+
d_ino = ino;
1758+
if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1759+
return -EOVERFLOW;
17561760
dirent = buf->previous;
17571761
if (dirent)
17581762
err = __put_user(offset, &dirent->d_off);
17591763
dirent = buf->current_dir;
17601764
err |= __put_user(dirent, &buf->previous);
1761-
err |= __put_user(ino, &dirent->d_ino);
1765+
err |= __put_user(d_ino, &dirent->d_ino);
17621766
err |= __put_user(reclen, &dirent->d_reclen);
17631767
err |= copy_to_user((char __user *)dirent->d_name, name, namlen) ? -EFAULT : 0;
17641768
err |= __put_user(0, &dirent->d_name[namlen]);
@@ -1837,7 +1841,7 @@ struct irix_dirent64_callback {
18371841
#define ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
18381842

18391843
static int irix_filldir64(void *__buf, const char *name,
1840-
int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1844+
int namlen, loff_t offset, u64 ino, unsigned int d_type)
18411845
{
18421846
struct irix_dirent64 __user *dirent;
18431847
struct irix_dirent64_callback * buf = __buf;

‎arch/parisc/hpux/fs.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,21 @@ static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
7777
{
7878
struct hpux_dirent * dirent;
7979
struct getdents_callback * buf = (struct getdents_callback *) __buf;
80+
ino_t d_ino;
8081
int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1);
8182

8283
buf->error = -EINVAL; /* only used if we fail.. */
8384
if (reclen > buf->count)
8485
return -EINVAL;
86+
d_ino = ino;
87+
if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
88+
return -EOVERFLOW;
8589
dirent = buf->previous;
8690
if (dirent)
8791
put_user(offset, &dirent->d_off);
8892
dirent = buf->current_dir;
8993
buf->previous = dirent;
90-
put_user(ino, &dirent->d_ino);
94+
put_user(d_ino, &dirent->d_ino);
9195
put_user(reclen, &dirent->d_reclen);
9296
put_user(namlen, &dirent->d_namlen);
9397
copy_to_user(dirent->d_name, name, namlen);

‎arch/parisc/kernel/sys_parisc32.c‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,19 @@ int sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user
237237

238238
int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
239239
{
240+
compat_ino_t ino;
240241
int err;
241242

242243
if (stat->size > MAX_NON_LFS || !new_valid_dev(stat->dev) ||
243244
!new_valid_dev(stat->rdev))
244245
return -EOVERFLOW;
245246

247+
ino = stat->ino;
248+
if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
249+
return -EOVERFLOW;
250+
246251
err = put_user(new_encode_dev(stat->dev), &statbuf->st_dev);
247-
err |= put_user(stat->ino, &statbuf->st_ino);
252+
err |= put_user(ino, &statbuf->st_ino);
248253
err |= put_user(stat->mode, &statbuf->st_mode);
249254
err |= put_user(stat->nlink, &statbuf->st_nlink);
250255
err |= put_user(0, &statbuf->st_reserved1);
@@ -312,16 +317,20 @@ filldir32 (void *__buf, const char *name, int namlen, loff_t offset, ino_t ino,
312317
struct linux32_dirent __user * dirent;
313318
struct getdents32_callback * buf = (struct getdents32_callback *) __buf;
314319
int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1, 4);
320+
u32 d_ino;
315321

316322
buf->error = -EINVAL; /* only used if we fail.. */
317323
if (reclen > buf->count)
318324
return -EINVAL;
325+
d_ino = ino;
326+
if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
327+
return -EOVERFLOW;
319328
dirent = buf->previous;
320329
if (dirent)
321330
put_user(offset, &dirent->d_off);
322331
dirent = buf->current_dir;
323332
buf->previous = dirent;
324-
put_user(ino, &dirent->d_ino);
333+
put_user(d_ino, &dirent->d_ino);
325334
put_user(reclen, &dirent->d_reclen);
326335
copy_to_user(dirent->d_name, name, namlen);
327336
put_user(0, dirent->d_name + namlen);
@@ -371,12 +380,16 @@ fillonedir32 (void * __buf, const char * name, int namlen, loff_t offset, ino_t
371380
{
372381
struct readdir32_callback * buf = (struct readdir32_callback *) __buf;
373382
struct old_linux32_dirent __user * dirent;
383+
u32 d_ino;
374384

375385
if (buf->count)
376386
return -EINVAL;
387+
d_ino = ino;
388+
if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
389+
return -EOVERFLOW;
377390
buf->count++;
378391
dirent = buf->dirent;
379-
put_user(ino, &dirent->d_ino);
392+
put_user(d_ino, &dirent->d_ino);
380393
put_user(offset, &dirent->d_offset);
381394
put_user(namlen, &dirent->d_namlen);
382395
copy_to_user(dirent->d_name, name, namlen);

‎arch/powerpc/kernel/sys_ppc32.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,20 @@ struct readdir_callback32 {
6969
};
7070

7171
static int fillonedir(void * __buf, const char * name, int namlen,
72-
off_t offset, ino_t ino, unsigned int d_type)
72+
off_t offset, u64 ino, unsigned int d_type)
7373
{
7474
struct readdir_callback32 * buf = (struct readdir_callback32 *) __buf;
7575
struct old_linux_dirent32 __user * dirent;
76+
ino_t d_ino;
7677

7778
if (buf->count)
7879
return -EINVAL;
80+
d_ino = ino;
81+
if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
82+
return -EOVERFLOW;
7983
buf->count++;
8084
dirent = buf->dirent;
81-
put_user(ino, &dirent->d_ino);
85+
put_user(d_ino, &dirent->d_ino);
8286
put_user(offset, &dirent->d_offset);
8387
put_user(namlen, &dirent->d_namlen);
8488
copy_to_user(dirent->d_name, name, namlen);
@@ -120,15 +124,20 @@ asmlinkage long ppc32_select(u32 n, compat_ulong_t __user *inp,
120124

121125
int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
122126
{
127+
compat_ino_t ino;
123128
long err;
124129

125130
if (stat->size > MAX_NON_LFS || !new_valid_dev(stat->dev) ||
126131
!new_valid_dev(stat->rdev))
127132
return -EOVERFLOW;
128133

134+
ino = stat->ino;
135+
if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
136+
return -EOVERFLOW;
137+
129138
err = access_ok(VERIFY_WRITE, statbuf, sizeof(*statbuf)) ? 0 : -EFAULT;
130139
err |= __put_user(new_encode_dev(stat->dev), &statbuf->st_dev);
131-
err |= __put_user(stat->ino, &statbuf->st_ino);
140+
err |= __put_user(ino, &statbuf->st_ino);
132141
err |= __put_user(stat->mode, &statbuf->st_mode);
133142
err |= __put_user(stat->nlink, &statbuf->st_nlink);
134143
err |= __put_user(stat->uid, &statbuf->st_uid);

‎arch/s390/kernel/compat_linux.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,16 @@ asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long high, unsigned
357357

358358
int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
359359
{
360+
compat_ino_t ino;
360361
int err;
361362

362363
if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
363364
return -EOVERFLOW;
364365

366+
ino = stat->ino;
367+
if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
368+
return -EOVERFLOW;
369+
365370
err = put_user(old_encode_dev(stat->dev), &statbuf->st_dev);
366371
err |= put_user(stat->ino, &statbuf->st_ino);
367372
err |= put_user(stat->mode, &statbuf->st_mode);

‎arch/sparc/kernel/sys_sunos.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,21 +325,25 @@ struct sunos_dirent_callback {
325325
#define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
326326

327327
static int sunos_filldir(void * __buf, const char * name, int namlen,
328-
loff_t offset, ino_t ino, unsigned int d_type)
328+
loff_t offset, u64 ino, unsigned int d_type)
329329
{
330330
struct sunos_dirent __user *dirent;
331331
struct sunos_dirent_callback * buf = __buf;
332+
unsigned long d_ino;
332333
int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1);
333334

334335
buf->error = -EINVAL; /* only used if we fail.. */
335336
if (reclen > buf->count)
336337
return -EINVAL;
338+
d_ino = ino;
339+
if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
340+
return -EOVERFLOW;
337341
dirent = buf->previous;
338342
if (dirent)
339343
put_user(offset, &dirent->d_off);
340344
dirent = buf->curr;
341345
buf->previous = dirent;
342-
put_user(ino, &dirent->d_ino);
346+
put_user(d_ino, &dirent->d_ino);
343347
put_user(namlen, &dirent->d_namlen);
344348
put_user(reclen, &dirent->d_reclen);
345349
copy_to_user(dirent->d_name, name, namlen);
@@ -406,19 +410,23 @@ struct sunos_direntry_callback {
406410
};
407411

408412
static int sunos_filldirentry(void * __buf, const char * name, int namlen,
409-
loff_t offset, ino_t ino, unsigned int d_type)
413+
loff_t offset, u64 ino, unsigned int d_type)
410414
{
411415
struct sunos_direntry __user *dirent;
412416
struct sunos_direntry_callback *buf = __buf;
417+
unsigned long d_ino;
413418
int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1);
414419

415420
buf->error = -EINVAL; /* only used if we fail.. */
416421
if (reclen > buf->count)
417422
return -EINVAL;
423+
d_ino = ino;
424+
if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
425+
return -EOVERFLOW;
418426
dirent = buf->previous;
419427
dirent = buf->curr;
420428
buf->previous = dirent;
421-
put_user(ino, &dirent->d_ino);
429+
put_user(d_ino, &dirent->d_ino);
422430
put_user(namlen, &dirent->d_namlen);
423431
put_user(reclen, &dirent->d_reclen);
424432
copy_to_user(dirent->d_name, name, namlen);

‎arch/sparc64/kernel/sys_sparc32.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,17 @@ asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long high, unsigned
337337

338338
int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
339339
{
340+
compat_ino_t ino;
340341
int err;
341342

342343
if (stat->size > MAX_NON_LFS || !old_valid_dev(stat->dev) ||
343344
!old_valid_dev(stat->rdev))
344345
return -EOVERFLOW;
345346

347+
ino = stat->ino;
348+
if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
349+
return -EOVERFLOW;
350+
346351
err = put_user(old_encode_dev(stat->dev), &statbuf->st_dev);
347352
err |= put_user(stat->ino, &statbuf->st_ino);
348353
err |= put_user(stat->mode, &statbuf->st_mode);

0 commit comments

Comments
 (0)