|
23 | 23 | #include "internal.h"
|
24 | 24 | #include "mount.h"
|
25 | 25 |
|
| 26 | +static DEFINE_IDR(pidfs_ino_idr); |
| 27 | + |
| 28 | +static u32 pidfs_ino_upper_32_bits = 0; |
| 29 | + |
| 30 | +#if BITS_PER_LONG == 32 |
| 31 | +/* |
| 32 | + * On 32 bit systems the lower 32 bits are the inode number and |
| 33 | + * the higher 32 bits are the generation number. The starting |
| 34 | + * value for the inode number and the generation number is one. |
| 35 | + */ |
| 36 | +static u32 pidfs_ino_lower_32_bits = 1; |
| 37 | + |
| 38 | +static inline unsigned long pidfs_ino(u64 ino) |
| 39 | +{ |
| 40 | + return lower_32_bits(ino); |
| 41 | +} |
| 42 | + |
| 43 | +/* On 32 bit the generation number are the upper 32 bits. */ |
| 44 | +static inline u32 pidfs_gen(u64 ino) |
| 45 | +{ |
| 46 | + return upper_32_bits(ino); |
| 47 | +} |
| 48 | + |
| 49 | +#else |
| 50 | + |
| 51 | +static u32 pidfs_ino_lower_32_bits = 0; |
| 52 | + |
| 53 | +/* On 64 bit simply return ino. */ |
| 54 | +static inline unsigned long pidfs_ino(u64 ino) |
| 55 | +{ |
| 56 | + return ino; |
| 57 | +} |
| 58 | + |
| 59 | +/* On 64 bit the generation number is 1. */ |
| 60 | +static inline u32 pidfs_gen(u64 ino) |
| 61 | +{ |
| 62 | + return 1; |
| 63 | +} |
| 64 | +#endif |
| 65 | + |
| 66 | +/* |
| 67 | + * Construct an inode number for struct pid in a way that we can use the |
| 68 | + * lower 32bit to lookup struct pid independent of any pid numbers that |
| 69 | + * could be leaked into userspace (e.g., via file handle encoding). |
| 70 | + */ |
| 71 | +int pidfs_add_pid(struct pid *pid) |
| 72 | +{ |
| 73 | + u32 upper; |
| 74 | + int lower; |
| 75 | + |
| 76 | + /* |
| 77 | + * Inode numbering for pidfs start at 2. This avoids collisions |
| 78 | + * with the root inode which is 1 for pseudo filesystems. |
| 79 | + */ |
| 80 | + lower = idr_alloc_cyclic(&pidfs_ino_idr, pid, 2, 0, GFP_ATOMIC); |
| 81 | + if (lower >= 0 && lower < pidfs_ino_lower_32_bits) |
| 82 | + pidfs_ino_upper_32_bits++; |
| 83 | + upper = pidfs_ino_upper_32_bits; |
| 84 | + pidfs_ino_lower_32_bits = lower; |
| 85 | + if (lower < 0) |
| 86 | + return lower; |
| 87 | + |
| 88 | + pid->ino = ((u64)upper << 32) | lower; |
| 89 | + pid->stashed = NULL; |
| 90 | + return 0; |
| 91 | +} |
| 92 | + |
| 93 | +/* The idr number to remove is the lower 32 bits of the inode. */ |
| 94 | +void pidfs_remove_pid(struct pid *pid) |
| 95 | +{ |
| 96 | + idr_remove(&pidfs_ino_idr, lower_32_bits(pid->ino)); |
| 97 | +} |
| 98 | + |
26 | 99 | #ifdef CONFIG_PROC_FS
|
27 | 100 | /**
|
28 | 101 | * pidfd_show_fdinfo - print information about a pidfd
|
@@ -346,7 +419,7 @@ static inline void pidfs_free_inum(unsigned long ino)
|
346 | 419 | #else
|
347 | 420 | static inline int pidfs_inum(struct pid *pid, unsigned long *ino)
|
348 | 421 | {
|
349 |
| - *ino = pid->ino; |
| 422 | + *ino = pidfs_ino(pid->ino); |
350 | 423 | return 0;
|
351 | 424 | }
|
352 | 425 | #define pidfs_free_inum(ino) ((void)(ino))
|
@@ -429,11 +502,14 @@ static const struct dentry_operations pidfs_dentry_operations = {
|
429 | 502 |
|
430 | 503 | static int pidfs_init_inode(struct inode *inode, void *data)
|
431 | 504 | {
|
| 505 | + const struct pid *pid = data; |
| 506 | + |
432 | 507 | inode->i_private = data;
|
433 | 508 | inode->i_flags |= S_PRIVATE;
|
434 | 509 | inode->i_mode |= S_IRWXU;
|
435 | 510 | inode->i_op = &pidfs_inode_operations;
|
436 | 511 | inode->i_fop = &pidfs_file_operations;
|
| 512 | + inode->i_generation = pidfs_gen(pid->ino); |
437 | 513 | /*
|
438 | 514 | * Inode numbering for pidfs start at RESERVED_PIDS + 1. This
|
439 | 515 | * avoids collisions with the root inode which is 1 for pseudo
|
|
0 commit comments