Skip to content

proc: fix a few old bugs in /proc handlers (Part 1/7)

Christian Brauner dri-devel kexec linux-acpi linux-alpha linux-cifs linux-debuggers linux-doc linux-ext4 linux-fbdev linux-fsdevel linux-hwmon linux-input linux-kselftest linux-m68k linux-mm linux-modules linux-nfs linux-pci linux-raid linux-rtc linux-s390 linux-scsi linux-serial linux-sh linux-sound linux-um linux-usb linux-wireless linuxppc-dev lkml netdev netfilter-devel ntfs3 oe-kbuild-all platform-driver-x86 sparclinux stable 3 versions – In review

Get series with b4
b4 am [email protected] change-id 20261101-work-procfs-bugfixes-054d00028491

Versions newest first

  1. v2 Latest posting 2/251 patches 8 repliesIn review
  2. v2 posting 1/239 of 51 patches 11 repliesSuperseded
  3. v1 RFC38 patches 29 repliesSuperseded

Changes in v2 from the cover letter · 16 lines

- thirteen more fixes that the review of v1 turned up: alloc_tag,
esas2r, cs46xx, isapnp, simdisk, pseries ofdt, lockd, lparcfg, psi,
the sparc LED, rtas progress, profiling and zorro
- the NOMMU, crash_dump and lparcfg patches say more about the bug
- new patch: sysctl readdir ended a listing early when an entry was
being unregistered, pre-existing
- the xt_recent patch is dropped, Cen Zhang's fix for the same lock is
on its way through netfilter
- the readdir churn selftests skip instead of failing without
/dev/net/tun, the sysctl removal selftest seeks past the dots first so
its fault lands in an entry, the selftests' Makefile, .gitignore and
config lines sit in the series that adds each test
- struct sysctl_dirent loses its unused mode
- Reviewed-by and Acked-by from Rafael, Takashi and Chuck
- rebased onto v7.4-rc1
- Link to v1: https://patch.msgid.link/[email protected]

Patches in v2 posting 2/2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/51alloc_tag: don't remove /proc/allocinfo under the codetag module lockmm/alloc_tag.c+24 -32 replies
2/51scsi: esas2r: don't copy kernel pointers out in EXPRESS_IOCTL_GET_MOD_INFOdrivers/scsi/esas2r/esas2r_ioctl.c+4 -30 replies
3/51ALSA: cs46xx: hand the region to the region proc filessound/pci/cs46xx/cs46xx_lib.c+1 -11 reply
4/51PNP: isapnp: don't go on when isapnp_cfg_begin() failsdrivers/pnp/isapnp/core.c drivers/pnp/isapnp/proc.c sound/isa/es18xx.c+21 -60 replies
5/51xtensa: simdisk: remove the proc entry before the diskarch/xtensa/platforms/iss/simdisk.c+2 -10 replies
6/51powerpc/pseries: put the node that the ofdt property commands look uparch/powerpc/platforms/pseries/reconfig.c+3 -30 replies
7/51lockd: don't hand out the terminator of nlm_end_gracefs/lockd/procfs.c+1 -10 replies
8/51powerpc/pseries/lparcfg: don't drop the last byte of a writearch/powerpc/platforms/pseries/lparcfg.c+2 -30 replies
9/51profiling: report it when /proc/profile can't be createdkernel/profile.c+4 -40 replies
10/51zorro: size the proc files like what they servedrivers/zorro/proc.c+1 -11 reply
11/51sparc: led: consume the whole writearch/sparc/kernel/led.c+2 -10 replies
12/51powerpc/rtas: consume the whole write to /proc/powerpc/rtas/progressarch/powerpc/kernel/rtas-proc.c+2 -10 replies
13/51sched/psi: don't drop the last byte of a trigger writekernel/sched/psi.c+2 -20 replies
14/51proc: sysctl: don't stop readdir at an entry that is being unregisteredfs/proc/proc_sysctl.c+3 -00 replies
15/51PCI/proc: don't truncate the position of config space accessesdrivers/pci/proc.c+8 -70 replies
16/51PNP: isapnp: don't read past the end of the buffer on a large pread()drivers/pnp/isapnp/proc.c+2 -40 replies
17/51s390/dasd: don't look in front of the buffer on an empty writedrivers/s390/block/dasd.c+1 -10 replies
18/51platform/x86: toshiba_acpi: reject malformed brightness and fan writesdrivers/platform/x86/toshiba_acpi.c+4 -40 replies
19/51xtensa: simdisk: don't read the file name without the lockarch/xtensa/platforms/iss/simdisk.c+12 -130 replies
20/51powerpc/rtas_flash: don't read past the validate messagearch/powerpc/kernel/rtas_flash.c+1 -10 replies
21/51powerpc/rtasd: zero the error log bufferarch/powerpc/kernel/rtasd.c+1 -10 replies
22/51ALSA: info: don't write past the end of a data entrysound/core/info.c+2 -00 replies
23/51scsi: BusLogic: don't return 0 from writes to the proc filedrivers/scsi/BusLogic.c+1 -10 replies
24/51scsi: ips: don't return 0 from writes to the proc filedrivers/scsi/ips.c+1 -10 replies
25/51wifi: libipw: don't return 0 from writes to debug_leveldrivers/net/wireless/intel/ipw2x00/libipw_module.c+1 -10 replies
26/51scsi: core: NUL-terminate what is written to the host proc filesdrivers/scsi/scsi_proc.c+1 -00 replies
27/51um: NUL-terminate what is written to /proc/exitcodearch/um/kernel/exitcode.c+2 -10 replies
28/51ntfs3: don't warn when a huge label is writtenfs/ntfs3/super.c+3 -80 replies
29/51proc: don't walk through a non-directory when resolving a namefs/proc/generic.c+1 -10 replies
30/51net: pktgen: reject device names with a '/'net/core/pktgen.c+4 -00 replies
31/51proc: don't WARN about a name that is already registeredfs/proc/generic.c+3 -20 replies
32/51proc: resume readdir after the last name it returnedfs/proc/generic.c fs/proc/internal.h fs/proc/proc_net.c +1 more+80 -120 replies
33/51selftests/proc: check a /proc listing while entries come and gotools/testing/selftests/proc/.gitignore tools/testing/selftests/proc/Makefile tools/testing/selftests/proc/config +1 more+246 -00 replies
34/51proc: let proc_remove() remove the entry it was givenfs/proc/generic.c+51 -270 replies
35/51proc: warn about entries left in /proc/net of a dying namespacefs/proc/proc_net.c+6 -00 replies
36/51proc: don't let NOMMU map procfs filesfs/proc/inode.c+24 -00 replies
37/51proc/vmcore: don't let vmcore_cleanup() run twice at oncefs/proc/vmcore.c+5 -00 replies
38/51powerpc/crash_dump: check ioremap_cache() in copy_oldmem_page()arch/powerpc/kernel/crash_dump.c+2 -00 replies
39/51powerpc/crash_dump: recover from a machine check in copy_oldmem_page()arch/powerpc/kernel/crash_dump.c+2 -21 reply
40/51sunrpc: make queue_wait a global wait queue againinclude/linux/sunrpc/cache.h net/sunrpc/cache.c+9 -40 replies
41/51scsi: esas2r: bound the lengths of the HBA, SMP and firmware ioctlsdrivers/scsi/esas2r/esas2r.h drivers/scsi/esas2r/esas2r_ioctl.c drivers/scsi/esas2r/esas2r_main.c+29 -170 replies
42/51powerpc/pseries: bound the lengths in the ofdt property parserarch/powerpc/platforms/pseries/reconfig.c+10 -80 replies
43/51powerpc/pseries/lparcfg: return h_get_ppp()'s status as a longarch/powerpc/platforms/pseries/lparcfg.c+1 -10 replies
44/51proc: sysctl: resume readdir after the last name it returnedfs/proc/proc_sysctl.c+103 -33 replies
45/51selftests/proc: check a sysctl listing while entries come and gotools/testing/selftests/proc/.gitignore tools/testing/selftests/proc/Makefile tools/testing/selftests/proc/proc-sysctl-readdir-churn.c+154 -00 replies
46/51proc: sysctl: copy to and from userspace without holding the tablefs/proc/proc_sysctl.c+28 -220 replies
47/51proc: sysctl: look at the table under sysctl_lock when creating an inodefs/proc/proc_sysctl.c+13 -70 replies
48/51proc: sysctl: don't hold the tables while readdir copies to userspacefs/proc/proc_sysctl.c+166 -1450 replies
49/51selftests/proc: check that faulting sysctl accesses don't hold up removaltools/testing/selftests/proc/.gitignore tools/testing/selftests/proc/Makefile tools/testing/selftests/proc/proc-remove-slow-sysctl.c+217 -00 replies
50/51proc: don't pin a symlink's entry while readlink() copies it outfs/proc/inode.c+7 -70 replies
51/51selftests/proc: check that a faulting readlink() doesn't hold up removaltools/testing/selftests/proc/.gitignore tools/testing/selftests/proc/Makefile tools/testing/selftests/proc/config +1 more+198 -00 replies

Reviews trailers per patch

7 of 51 patches have a Reviewed-by or Acked-by.

PatchRafael J. Wysocki (Intel)Takashi IwaiChuck LeverGeert UytterhoevenJann HornBreno Leitao
1/51
2/51
3/51R-b
4/51
5/51
6/51
7/51
8/51
9/51
10/51R-b A-b
11/51
12/51
13/51
14/51
15/51
16/51R-b
17/51
18/51
19/51
20/51
21/51
22/51R-b
23/51
24/51
25/51
26/51
27/51
28/51
29/51
30/51
31/51
32/51
33/51
34/51
35/51
36/51
37/51
38/51
39/51R-b
40/51A-b
41/51
42/51
43/51
44/51R-b
45/51
46/51
47/51
48/51
49/51
50/51
51/51

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 6 reviewers.

Read the v2 thread

Cover letter [PATCH RESEND v2 00/51] proc: fix a few old bugs in /proc handlers (Part 1/7)

Part 1/7 of "proc: move procfs to what kernfs does", the base of the other six parts:

https://patch.msgid.link/20261104-work-procfs-v2-0-b45cd5789ac5@brauner

Moving procfs to what kernfs does touches just about every ->proc_read(), ->proc_write() and ->proc_ioctl() in the tree. Reviewing that turned up bugs that have nothing to do with the conversion and are a lot older. They go first so they can go to stable on their own. Four selftests come with them.

In procfs itself:

- readdir resumed a listing at a counted position, so an entry that came or went in between repeated or skipped one, and it ended a listing early when the entry it was emitting went away. It resumes after the last name now, like kernfs. /proc/sys had the same bug.
- /proc/sys readdir ended a listing early at an entry that was being unregistered, so getdents64() reported EOF with entries left.
- /proc/sys copied to and from userspace with the table held, so a read, write or getdents64() that faults for as long as userspace wants kept unregister_sysctl_table() waiting, which addrconf calls under RTNL.
- proc_sys_make_inode() read the table after it had dropped sysctl_lock.
- a symlink's entry stayed pinned while readlink() copied it out, so a faulting readlink() held up remove_proc_entry(). ALSA removes its card links with snd_card_mutex held.
- name resolution walked through regular files, and pktgen lets userspace pick names, so an unprivileged user could hang an entry below another file and get it freed. pktgen refuses a '/' now.
- proc_register() WARNed on a duplicate name, which pktgen's names can provoke.
- proc_remove() removed by name, so removing an entry that was already gone could take out a different one.
- the parent of entries a dying network namespace left in /proc/net was freed silently. procfs warns now.
- on NOMMU every /proc file could be mapped privately, which reads it with nommu_region_sem held, and /proc/maps takes that lock. Any user could hang the machine.
- sunrpc queued the pollers of its channel files on a wait queue in the per-namespace cache_detail, which is freed with the namespace. Back to one static queue here, the conversion makes procfs own the queues.
- vmcore_cleanup() could run twice at once.

In the handlers:

- PCI/proc and isapnp truncated the position to an int, so a pwrite() or pread() at a huge offset went negative and accessed config space far outside the file.
- dasd looked in front of its buffer on an empty write.
- toshiba_acpi's brightness and fan write checks could never fail.
- simdisk read the file name without the lock that protects it, so a reader could copy out memory a concurrent detach had freed.
- rtas_flash read past its validate message, and rtasd handed out what vmalloc() left in its log buffer.
- ALSA's info layer let a write past the end of a data entry through unclamped.
- BusLogic, ips and libipw returned 0 from writes, so stdio writers retry forever.
- the scsi host files and um's /proc/exitcode parsed writes without NUL-terminating them.
- ntfs3 tripped the page allocator's warning on a huge label write.
- esas2r took the lengths of its HBA, SMP and firmware ioctls from userspace unchecked, through a file everyone can open.
- powerpc's copy_oldmem_page() didn't check ioremap_cache() and only recovered from a machine check for read(), the pseries ofdt parser read past its input, and lparcfg reported a failed hcall as a huge write.
- alloc_tag removed /proc/allocinfo with the codetag module lock held when a module's tags couldn't be reserved, and a reader of the file takes that lock with the entry pinned, so the module load and the reader waited for each other for good.
- esas2r copied the kernel addresses of the adapter, its pci_dev and its Scsi_Host out through the 0444 ATTONode.
- cs46xx stored the chip in the private data of its region files and read it back as the region.
- isapnp went on talking to whatever device the card had selected, and unlocked a mutex it never took, when isapnp_cfg_begin() refused a logical device number.
- simdisk freed the gendisk before it removed the entry, so a write in between set the capacity of a freed disk.
- the pseries ofdt commands leaked a node reference per property command, and lockd handed out the NUL behind "Y\n".
- lparcfg and psi overwrote the last byte of a write to terminate it, so a write without a trailing newline lost a character.
- the sparc LED and rtas progress files consumed only the start of a write, so a looping writer sent the rest as a second command.
- profiling reported success when /proc/profile couldn't be created, and zorro sized its files like a zorro_dev instead of the ConfigDev they serve.

The xt_recent deadlock v1 fixed here goes in through netfilter instead.

The procfs, sysctl, readlink, pktgen, sunrpc, PCI and duplicate name bugs reproduce on a KASAN and lockdep kernel in a VM and are gone with the series. The rest is build tested.

---
Changes in v2:
- thirteen more fixes that the review of v1 turned up: alloc_tag,
esas2r, cs46xx, isapnp, simdisk, pseries ofdt, lockd, lparcfg, psi,
the sparc LED, rtas progress, profiling and zorro
- the NOMMU, crash_dump and lparcfg patches say more about the bug
- new patch: sysctl readdir ended a listing early when an entry was
being unregistered, pre-existing
- the xt_recent patch is dropped, Cen Zhang's fix for the same lock is
on its way through netfilter
- the readdir churn selftests skip instead of failing without
/dev/net/tun, the sysctl removal selftest seeks past the dots first so
its fault lands in an entry, the selftests' Makefile, .gitignore and
config lines sit in the series that adds each test
- struct sysctl_dirent loses its unused mode
- Reviewed-by and Acked-by from Rafael, Takashi and Chuck
- rebased onto v7.4-rc1
- Link to v1: https://patch.msgid.link/[email protected]

---
Christian Brauner (51):
alloc_tag: don't remove /proc/allocinfo under the codetag module lock
scsi: esas2r: don't copy kernel pointers out in EXPRESS_IOCTL_GET_MOD_INFO
ALSA: cs46xx: hand the region to the region proc files
PNP: isapnp: don't go on when isapnp_cfg_begin() fails
xtensa: simdisk: remove the proc entry before the disk powerpc/pseries: put the node that the ofdt property commands look up
lockd: don't hand out the terminator of nlm_end_grace powerpc/pseries/lparcfg: don't drop the last byte of a write
profiling: report it when /proc/profile can't be created
zorro: size the proc files like what they serve
sparc: led: consume the whole write
powerpc/rtas: consume the whole write to /proc/powerpc/rtas/progress sched/psi: don't drop the last byte of a trigger write
proc: sysctl: don't stop readdir at an entry that is being unregistered PCI/proc: don't truncate the position of config space accesses
PNP: isapnp: don't read past the end of the buffer on a large pread() s390/dasd: don't look in front of the buffer on an empty write platform/x86: toshiba_acpi: reject malformed brightness and fan writes
xtensa: simdisk: don't read the file name without the lock powerpc/rtas_flash: don't read past the validate message powerpc/rtasd: zero the error log buffer
ALSA: info: don't write past the end of a data entry
scsi: BusLogic: don't return 0 from writes to the proc file
scsi: ips: don't return 0 from writes to the proc file
wifi: libipw: don't return 0 from writes to debug_level
scsi: core: NUL-terminate what is written to the host proc files
um: NUL-terminate what is written to /proc/exitcode
ntfs3: don't warn when a huge label is written
proc: don't walk through a non-directory when resolving a name
net: pktgen: reject device names with a '/'
proc: don't WARN about a name that is already registered
proc: resume readdir after the last name it returned selftests/proc: check a /proc listing while entries come and go
proc: let proc_remove() remove the entry it was given
proc: warn about entries left in /proc/net of a dying namespace
proc: don't let NOMMU map procfs files
proc/vmcore: don't let vmcore_cleanup() run twice at once powerpc/crash_dump: check ioremap_cache() in copy_oldmem_page() powerpc/crash_dump: recover from a machine check in copy_oldmem_page()
sunrpc: make queue_wait a global wait queue again
scsi: esas2r: bound the lengths of the HBA, SMP and firmware ioctls powerpc/pseries: bound the lengths in the ofdt property parser powerpc/pseries/lparcfg: return h_get_ppp()'s status as a long
proc: sysctl: resume readdir after the last name it returned selftests/proc: check a sysctl listing while entries come and go
proc: sysctl: copy to and from userspace without holding the table
proc: sysctl: look at the table under sysctl_lock when creating an inode
proc: sysctl: don't hold the tables while readdir copies to userspace selftests/proc: check that faulting sysctl accesses don't hold up removal
proc: don't pin a symlink's entry while readlink() copies it out selftests/proc: check that a faulting readlink() doesn't hold up removal

 arch/powerpc/kernel/crash_dump.c                   |   6 +-
 arch/powerpc/kernel/rtas-proc.c                    |   3 +-
 arch/powerpc/kernel/rtas_flash.c                   |   2 +-
 arch/powerpc/kernel/rtasd.c                        |   2 +-
 arch/powerpc/platforms/pseries/lparcfg.c           |   7 +-
 arch/powerpc/platforms/pseries/reconfig.c          |  24 +-
 arch/sparc/kernel/led.c                            |   3 +-
 arch/um/kernel/exitcode.c                          |   3 +-
 arch/xtensa/platforms/iss/simdisk.c                |  28 +-
 drivers/net/wireless/intel/ipw2x00/libipw_module.c |   2 +-
 drivers/pci/proc.c                                 |  15 +-
 drivers/platform/x86/toshiba_acpi.c                |   8 +-
 drivers/pnp/isapnp/core.c                          |  16 +-
 drivers/pnp/isapnp/proc.c                          |  11 +-
 drivers/s390/block/dasd.c                          |   2 +-
 drivers/scsi/BusLogic.c                            |   2 +-
 drivers/scsi/esas2r/esas2r.h                       |   2 +-
 drivers/scsi/esas2r/esas2r_ioctl.c                 |  48 ++-
 drivers/scsi/esas2r/esas2r_main.c                  |   3 +-
 drivers/scsi/ips.c                                 |   2 +-
 drivers/scsi/scsi_proc.c                           |   1 +
 drivers/zorro/proc.c                               |   2 +-
 fs/lockd/procfs.c                                  |   2 +-
 fs/ntfs3/super.c                                   |  11 +-
 fs/proc/generic.c                                  | 174 ++++++---
 fs/proc/inode.c                                    |  38 +-
 fs/proc/internal.h                                 |   1 +
 fs/proc/proc_net.c                                 |   7 +
 fs/proc/proc_sysctl.c                              | 388 ++++++++++++++-------
 fs/proc/root.c                                     |   1 +
 fs/proc/vmcore.c                                   |   5 +
 include/linux/sunrpc/cache.h                       |   1 -
 kernel/profile.c                                   |   8 +-
 kernel/sched/psi.c                                 |   4 +-
 mm/alloc_tag.c                                     |  27 +-
 net/core/pktgen.c                                  |   4 +
 net/sunrpc/cache.c                                 |  12 +-
 sound/core/info.c                                  |   2 +
 sound/isa/es18xx.c                                 |   6 +-
 sound/pci/cs46xx/cs46xx_lib.c                      |   2 +-
 tools/testing/selftests/proc/.gitignore            |   4 +
 tools/testing/selftests/proc/Makefile              |   4 +
 tools/testing/selftests/proc/config                |   9 +
 tools/testing/selftests/proc/proc-readdir-churn.c  | 240 +++++++++++++
 .../selftests/proc/proc-remove-slow-readlink.c     | 191 ++++++++++
 .../selftests/proc/proc-remove-slow-sysctl.c       | 215 ++++++++++++
 .../selftests/proc/proc-sysctl-readdir-churn.c     | 152 ++++++++
 47 files changed, 1420 insertions(+), 280 deletions(-)

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261101-work-procfs-bugfixes-054d00028491