proc: fix a few old bugs in /proc handlers (Part 1/7)
Subsystems (50)
Christian Brauner
maintainerv2 cover proc: fix a few old bugs in /proc handlers (Part 1/7)
In reply to a message not in the archivePart 1/7 of "proc: move procfs to what kernfs does", the base of the other six parts:
https://patch.msgid.link/[email protected]
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
Christian Brauner
maintainerPATCH v2 1/51 alloc_tag: don't remove /proc/allocinfo under the codetag module lock
codetag_alloc_module_section() takes the codetag type's mod_lock for writing and calls reserve_module_tags() with it held. When that can't reserve the tags, because the section's alignment doesn't fit, because there are too many tags for the page flag bits or because the memory for them can't be allocated, it shuts memory allocation profiling down and removes /proc/allocinfo, still with mod_lock held.
remove_proc_entry() waits until every reader of the entry has let go of it, and a reader of /proc/allocinfo takes mod_lock for reading in allocinfo_start() while it holds the entry. So a module load that hits one of those paths while /proc/allocinfo is being read waits for the reader and the reader waits for the module load, for good.
Let a work item remove the entry in that case. The profiling is shut down right away like before, the file just goes a moment later.
--- mm/alloc_tag.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-)
mm/alloc_tag.c +24 -3
@@ -15,6 +15,7 @@#include <linux/seq_file.h> #include <linux/string_choices.h> #include <linux/vmalloc.h> +#include <linux/workqueue.h> #include <linux/kmemleak.h> #include <uapi/linux/alloc_tag.h>
@@ -604,6 +605,26 @@ static void shutdown_mem_profiling(bool remove_file) mem_profiling_support = false; } +static void remove_allocinfo_file (struct work_struct *work) +{ +remove_proc_entry (ALLOCINFO_FILE_NAME ,NULL ); +} +static DECLARE_WORK(remove_allocinfo_work, remove_allocinfo_file); + +/* +* A module's tags are reserved with the codetag module lock held for +* writing, and a reader of /proc/allocinfo takes that lock with the entry +* pinned. Removing the entry there would wait for that reader for good, so +* let a work item do it. +*/ +static void shutdown_mem_profiling_async (void ) +{ +if (!mem_profiling_support) +return ; +shutdown_mem_profiling (false); +schedule_work (&remove_allocinfo_work); +} +void __init alloc_tag_sec_init (void ) {struct alloc_tag *last_codetag;
@@ -855,7 +876,7 @@ static void *reserve_module_tags(struct module *mod, unsigned long size, align =1 ;if (!ensure_alignment (align, &prepend)) { -shutdown_mem_profiling (true); +shutdown_mem_profiling_async ();pr_err ("%s: alignment %lu is incompatible with allocation tag indexing. Memory allocation profiling is disabled!\n" , mod->name, align);return ERR_PTR (-EINVAL );
@@ -905,14 +926,14 @@ unlock: module_tags.size = offset + size;if (mem_alloc_profiling_enabled () && !tags_addressable ()) { -shutdown_mem_profiling (true); +shutdown_mem_profiling_async ();pr_warn ("With module %s there are too many tags to fit in %d page flag bits. Memory allocation profiling is disabled!\n" , mod->name,NR_UNUSED_PAGEFLAG_BITS ); } grow_res =vm_module_tags_populate ();if (grow_res) { -shutdown_mem_profiling (true); +shutdown_mem_profiling_async ();pr_err ("Failed to allocate memory for allocation tags in the module %s. Memory allocation profiling is disabled!\n" , mod->name);return ERR_PTR (grow_res);
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 2/51 scsi: esas2r: don't copy kernel pointers out in EXPRESS_IOCTL_GET_MOD_INFO
In reply to Christian BraunerEXPRESS_IOCTL_GET_MOD_INFO fills in the addresses of the adapter, its pci_dev and its Scsi_Host and copies the whole struct back to userspace. Nothing in userspace can do anything with those addresses, and the ioctl is reachable through /proc/scsi/esas2r/ATTONode, which is created with mode 0444. Only pass the host number.
--- drivers/scsi/esas2r/esas2r_ioctl.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/scsi/esas2r/esas2r_ioctl.c +4 -3
@@ -1468,9 +1468,10 @@ int esas2r_ioctl_handler(void *hostdata, unsigned int cmd, void __user *arg)case EXPRESS_IOCTL_GET_MOD_INFO : - ioctl->data.modinfo.adapter = a; - ioctl->data.modinfo.pci_dev = a->pcid; - ioctl->data.modinfo.scsi_host = a->host; +/* these are kernel pointers, userspace has no use for them */ + ioctl->data.modinfo.adapter =NULL ; + ioctl->data.modinfo.pci_dev =NULL ; + ioctl->data.modinfo.scsi_host =NULL ; ioctl->data.modinfo.host_no = a->host->host_no;break ;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 3/51 ALSA: cs46xx: hand the region to the region proc files
In reply to Christian Braunersnd_cs46xx_proc_init() stores the chip in the private data of the five region entries, but snd_cs46xx_io_read() reads that back as the region, so a read of /proc/asound/cardN/CS46xx_BA0 and friends copies from whatever sits at the region's offset in struct snd_cs46xx instead of the mapped registers. Store the region.
--- sound/pci/cs46xx/cs46xx_lib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
sound/pci/cs46xx/cs46xx_lib.c +1 -1
@@ -2764,7 +2764,7 @@ static int snd_cs46xx_proc_init(struct snd_card *card, struct snd_cs46xx *chip)struct snd_cs46xx_region *region = &chip->region.idx[idx];if (!snd_card_proc_new (card, region->name, &entry)) { entry->content =SNDRV_INFO_CONTENT_DATA ; - entry->private_data = chip; + entry->private_data = region; entry->c.ops = &snd_cs46xx_proc_io_ops; entry->size = region->size; entry->mode =S_IFREG |0400 ;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 4/51 PNP: isapnp: don't go on when isapnp_cfg_begin() fails
In reply to Christian Braunerisapnp_cfg_begin() refuses a logical device number above 10 and a bad card number with -EINVAL before it takes isapnp_cfg_mutex. The callers that read and write the device's configuration, the proc file included, don't look at that: they go on talking to whatever device the card currently has selected and then unlock a mutex they never took in isapnp_cfg_end(). isapnp_create_device() hands out logical device numbers without a bound, so a card with twelve or more logical devices gets there. Return the error instead.
--- drivers/pnp/isapnp/core.c | 16 ++++++++++++---- drivers/pnp/isapnp/proc.c | 5 ++++- sound/isa/es18xx.c | 6 +++++- 3 files changed, 21 insertions(+), 6 deletions(-)
drivers/pnp/isapnp/core.c +12 -4
@@ -853,7 +853,9 @@ static int isapnp_get_resources(struct pnp_dev *dev)pnp_dbg (&dev->dev,"get resources\n" );pnp_init_resources (dev); -isapnp_cfg_begin (dev->card->number, dev->number); + ret =isapnp_cfg_begin (dev->card->number, dev->number); +if (ret) +return ret; dev->active =isapnp_read_byte (ISAPNP_CFG_ACTIVATE );if (!dev->active) goto __end;
@@ -887,10 +889,12 @@ __end:static int isapnp_set_resources (struct pnp_dev *dev) {struct resource *res; -int tmp; +int tmp, ret;pnp_dbg (&dev->dev,"set resources\n" ); -isapnp_cfg_begin (dev->card->number, dev->number); + ret =isapnp_cfg_begin (dev->card->number, dev->number); +if (ret) +return ret; dev->active =1 ;for (tmp =0 ; tmp <ISAPNP_MAX_PORT ; tmp++) { res =pnp_get_resource (dev,IORESOURCE_IO , tmp);
@@ -936,9 +940,13 @@ static int isapnp_set_resources(struct pnp_dev *dev)static int isapnp_disable_resources (struct pnp_dev *dev) { +int ret; +if (!dev->active)return -EINVAL ; -isapnp_cfg_begin (dev->card->number, dev->number); + ret =isapnp_cfg_begin (dev->card->number, dev->number); +if (ret) +return ret;isapnp_deactivate (dev->number); dev->active =0 ;isapnp_cfg_end ();
drivers/pnp/isapnp/proc.c +4 -1
@@ -25,6 +25,7 @@ static ssize_t isapnp_proc_bus_read(struct file *file, char __user * buf,struct pnp_dev *dev =pde_data (file_inode (file));int pos = *ppos;int cnt, size =256 ; +int ret;if (pos >= size)return 0 ;
@@ -37,7 +38,9 @@ static ssize_t isapnp_proc_bus_read(struct file *file, char __user * buf,if (!access_ok (buf, cnt))return -EINVAL ; -isapnp_cfg_begin (dev->card->number, dev->number); + ret =isapnp_cfg_begin (dev->card->number, dev->number); +if (ret) +return ret;for (; pos <256 && cnt >0 ; pos++, buf++, cnt--) {unsigned char val; val =isapnp_read_byte (pos);
sound/isa/es18xx.c +5 -1
@@ -1950,7 +1950,11 @@ static int snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)/* ok. hack using Vendor-Defined Card-Level registers */ /* skip csn and logdev initialization - already done in isapnp_configure */ if (pnp_device_is_isapnp (pdev)) { -isapnp_cfg_begin (isapnp_card_number (pdev),isapnp_csn_number (pdev)); +int ret =isapnp_cfg_begin (isapnp_card_number (pdev), +isapnp_csn_number (pdev)); + +if (ret) +return ret;isapnp_write_byte (0x27 ,pnp_irq (pdev,0 ));/* Hardware Volume IRQ Number */ if (mpu_port[dev] !=SNDRV_AUTO_PORT )isapnp_write_byte (0x28 ,pnp_irq (pdev,0 ));/* MPU-401 IRQ Number */
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 5/51 xtensa: simdisk: remove the proc entry before the disk
In reply to Christian Braunersimdisk_teardown() frees the gendisk and only then removes /proc/simdisk/<n>. A write to that entry in between attaches the file that was written to the disk, and simdisk_attach() sets the capacity of the gendisk that is gone. Remove the entry first, remove_proc_entry() waits for a writer that is in the middle of that.
--- arch/xtensa/platforms/iss/simdisk.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/xtensa/platforms/iss/simdisk.c +2 -1
@@ -379,12 +379,13 @@ static void simdisk_teardown(struct simdisk *dev, int which, {char tmp[2 ] = {'0' + which,0 }; +/* a write to the entry attaches a file to the disk, so it goes first */ +remove_proc_entry (tmp, procdir);simdisk_detach (dev);if (dev->gd) {del_gendisk (dev->gd);put_disk (dev->gd); } -remove_proc_entry (tmp, procdir); } static void __exit simdisk_exit(void)
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 6/51 powerpc/pseries: put the node that the ofdt property commands look up
In reply to Christian Braunerparse_node() looks the node up with of_find_node_by_phandle(), which returns it with a reference, and do_add_property(), do_remove_property() and do_update_property() never put that reference. Every property command written to /proc/powerpc/ofdt leaks one. Put it when the command is done.
--- arch/powerpc/platforms/pseries/reconfig.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/powerpc/platforms/pseries/reconfig.c +3 -3
@@ -271,8 +271,8 @@ static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)static int do_add_property (char *buf,size_t bufsize) { + struct device_node *np __free(device_node) = NULL;struct property *prop =NULL ; -struct device_node *np;unsigned char *value;char *name, *end;int length;
@@ -296,7 +296,7 @@ static int do_add_property(char *buf, size_t bufsize)static int do_remove_property (char *buf,size_t bufsize) { -struct device_node *np; + struct device_node *np __free(device_node) = NULL;char *tmp; buf =parse_node (buf, bufsize, &np);
@@ -315,7 +315,7 @@ static int do_remove_property(char *buf, size_t bufsize)static int do_update_property (char *buf,size_t bufsize) { -struct device_node *np; + struct device_node *np __free(device_node) = NULL;unsigned char *value;char *name, *end, *next_prop;int length;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 7/51 lockd: don't hand out the terminator of nlm_end_grace
In reply to Christian Braunernlm_end_grace_read() answers with "Y\n" or "N\n" from a three byte buffer and hands out all three bytes, the terminating NUL included. Leave the NUL out.
--- fs/lockd/procfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/lockd/procfs.c +1 -1
@@ -57,7 +57,7 @@ nlm_end_grace_read(struct file *file, char __user *buf, size_t size, resp[1 ] ='\n' ; resp[2 ] ='\0' ; -return simple_read_from_buffer (buf, size, pos, resp,sizeof (resp)); +return simple_read_from_buffer (buf, size, pos, resp,sizeof (resp) -1 ); }static const struct proc_ops lockd_end_grace_proc_ops = {
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 8/51 powerpc/pseries/lparcfg: don't drop the last byte of a write
In reply to Christian Braunerlparcfg_write() terminates its copy of the write by overwriting the last byte, so a write without a trailing newline loses its last
character: "partition_entitled_capacity=5" is parsed as an empty value and fails with -EINVAL. Terminate behind the copy instead, and refuse a write that fills the whole buffer, which can't be terminated.
--- arch/powerpc/platforms/pseries/lparcfg.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
arch/powerpc/platforms/pseries/lparcfg.c +2 -3
@@ -699,13 +699,12 @@ static ssize_t lparcfg_write(struct file *file, const char __user * buf,if (!firmware_has_feature (FW_FEATURE_SPLPAR ))return -EINVAL ; -if (count ==0 || count >sizeof (kbuf)) +if (count ==0 || count >=sizeof (kbuf))return -EINVAL ;if (copy_from_user (kbuf, buf, count))return -EFAULT ; - - kbuf[count -1 ] ='\0' ; + kbuf[count] ='\0' ; tmp =strchr (kbuf,'=' );if (!tmp)return -EINVAL ;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 9/51 profiling: report it when /proc/profile can't be created
In reply to Christian Braunercreate_proc_profile() returns 0 whether proc_create() worked or not, so a write to /sys/kernel/profiling that turned profiling on but didn't get the file reports success and there is no file to read the profile from. Return -ENOMEM like the other failures of that write do.
--- kernel/profile.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/profile.c +4 -4
@@ -216,15 +216,15 @@ static const struct proc_ops profile_proc_ops = {int __ref create_proc_profile (void ) {struct proc_dir_entry *entry; -int err =0 ;if (!prof_on)return 0 ; entry =proc_create ("profile" ,S_IWUSR |S_IRUGO ,NULL , &profile_proc_ops); -if (entry) -proc_set_size (entry, (1 + prof_len) *sizeof (atomic_t)); -return err; +if (!entry) +return -ENOMEM ; +proc_set_size (entry, (1 + prof_len) *sizeof (atomic_t)); +return 0 ; }subsys_initcall (create_proc_profile);#endif /* CONFIG_PROC_FS */
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 10/51 zorro: size the proc files like what they serve
In reply to Christian BraunerThe files under /proc/bus/zorro hand out a struct ConfigDev and seek within that, but their size says sizeof(struct zorro_dev). Say sizeof(struct ConfigDev).
--- drivers/zorro/proc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/zorro/proc.c +1 -1
@@ -108,7 +108,7 @@ static int __init zorro_proc_attach_device(unsigned int slot) &zorro_autocon[slot]);if (!entry)return -ENOMEM ; -proc_set_size (entry,sizeof (struct zorro_dev )); +proc_set_size (entry,sizeof (struct ConfigDev ));return 0 ; }
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 11/51 sparc: led: consume the whole write
In reply to Christian Braunerled_proc_write() looks at the first eight bytes of a write and returns that many, so a writer that loops until everything is written, stdio does, sends the rest as a second command. That one is unknown and switches the LED off. Return the whole count, the rest is ignored.
--- arch/sparc/kernel/led.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/sparc/kernel/led.c +2 -1
@@ -68,6 +68,7 @@ static int led_proc_open(struct inode *inode, struct file *file)static ssize_t led_proc_write (struct file *file,const char __user *buffer,size_t count,loff_t *ppos) { +size_t len = count;char *buf =NULL ;if (count >LED_MAX_LENGTH )
@@ -102,7 +103,7 @@ static ssize_t led_proc_write(struct file *file, const char __user *buffer,kfree (buf); -return count; +return len; }static const struct proc_ops led_proc_ops = {
Signature · 1 line
2.53.0
Re: [PATCH v2 04/51] PNP: isapnp: don't go on when isapnp_cfg_begin() fails
In reply to Christian BraunerQuoted 10 lines · Christian Brauner
isapnp_cfg_begin()refuses a logical device number above 10 and a bad card number with-EINVALbefore it takesisapnp_cfg_mutex. The callers that read and write the device's configuration, the proc file included, don't look at that: they go on talking to whatever device the card currently has selected and then unlock a mutex they never took inisapnp_cfg_end().isapnp_create_device()hands out logical device numbers without a bound, so a card with twelve or more logical devices gets there. Return the error instead.
Quoted 3 lines
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Quoted 96 lines
--- drivers/pnp/isapnp/core.c | 16 ++++++++++++---- drivers/pnp/isapnp/proc.c | 5 ++++- sound/isa/es18xx.c | 6 +++++- 3 files changed, 21 insertions(+), 6 deletions(-)drivers/pnp/isapnp/core.c +12
-4@@ -853,7 +853,9 @@ static int isapnp_get_resources(struct pnp_dev *dev)pnp_dbg (&dev->dev,"get resources\n" );pnp_init_resources (dev); -isapnp_cfg_begin (dev->card->number, dev->number); + ret =isapnp_cfg_begin (dev->card->number, dev->number); +if (ret) +return ret; dev->active =isapnp_read_byte (ISAPNP_CFG_ACTIVATE );if (!dev->active) goto __end;@@ -887,10 +889,12 @@ __end:static int isapnp_set_resources (struct pnp_dev *dev) {struct resource *res; -int tmp; +int tmp, ret;pnp_dbg (&dev->dev,"set resources\n" ); -isapnp_cfg_begin (dev->card->number, dev->number); + ret =isapnp_cfg_begin (dev->card->number, dev->number); +if (ret) +return ret; dev->active =1 ;for (tmp =0 ; tmp <ISAPNP_MAX_PORT ; tmp++) { res =pnp_get_resource (dev,IORESOURCE_IO , tmp);@@ -936,9 +940,13 @@ static int isapnp_set_resources(struct pnp_dev *dev)static int isapnp_disable_resources (struct pnp_dev *dev) { +int ret; +if (!dev->active)return -EINVAL ; -isapnp_cfg_begin (dev->card->number, dev->number); + ret =isapnp_cfg_begin (dev->card->number, dev->number); +if (ret) +return ret;isapnp_deactivate (dev->number); dev->active =0 ;isapnp_cfg_end ();drivers/pnp/isapnp/proc.c +4
-1@@ -25,6 +25,7 @@ static ssize_t isapnp_proc_bus_read(struct file *file, char __user * buf,struct pnp_dev *dev =pde_data (file_inode (file));int pos = *ppos;int cnt, size =256 ; +int ret;if (pos >= size)return 0 ;@@ -37,7 +38,9 @@ static ssize_t isapnp_proc_bus_read(struct file *file, char __user * buf,if (!access_ok (buf, cnt))return -EINVAL ; -isapnp_cfg_begin (dev->card->number, dev->number); + ret =isapnp_cfg_begin (dev->card->number, dev->number); +if (ret) +return ret;for (; pos <256 && cnt >0 ; pos++, buf++, cnt--) {unsigned char val; val =isapnp_read_byte (pos);sound/isa/es18xx.c +5
-2@@ -1950,7 +1950,11 @@ static int snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)/* ok. hack using Vendor-Defined Card-Level registers */ /* skip csn and logdev initialization - already done in isapnp_configure */ if (pnp_device_is_isapnp (pdev)) { -isapnp_cfg_begin (isapnp_card_number (pdev),isapnp_csn_number (pdev)); +int ret =isapnp_cfg_begin (isapnp_card_number (pdev), +isapnp_csn_number (pdev)); + +if (ret) +return ret;isapnp_write_byte (0x27 ,pnp_irq (pdev,0 ));/* Hardware Volume IRQ Number */ if (mpu_port[dev] !=SNDRV_AUTO_PORT )isapnp_write_byte (0x28 ,pnp_irq (pdev,0 ));/* MPU-401 IRQ Number */ --2.53.0
Christian Brauner
maintainerPATCH v2 12/51 powerpc/rtas: consume the whole write to /proc/powerpc/rtas/progress
In reply to Christian Braunerppc_rtas_progress_write() shows the first 255 bytes of a write and returns that many, so a writer that loops until everything is written shows the rest as a second message. Return the whole count, the rest is ignored.
--- arch/powerpc/kernel/rtas-proc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/powerpc/kernel/rtas-proc.c +2 -1
@@ -311,6 +311,7 @@ static int ppc_rtas_poweron_show(struct seq_file *m, void *v)static ssize_t ppc_rtas_progress_write (struct file *file,const char __user *buf,size_t count,loff_t *ppos) { +size_t len = count;unsigned long hex;if (count >=MAX_LINELENGTH )
@@ -324,7 +325,7 @@ static ssize_t ppc_rtas_progress_write(struct file *file, hex =simple_strtoul (progress_led,NULL ,10 );rtas_progress ((char *)progress_led, hex); -return count; +return len;/* clear the line */ /* rtas_progress(" ", 0xffff);*/
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 13/51 sched/psi: don't drop the last byte of a trigger write
In reply to Christian Braunerpsi_write() copies at most 32 bytes of the write into its buffer and terminates the buffer by overwriting its last byte. For a write shorter than 32 bytes that is the last byte the writer sent. A trigger written without a trailing newline loses its last digit then, "some 150000 1000000" turns into "some 150000 100000", and a shorter one like "some 150 10000" fails with -EINVAL because the window doesn't fit anymore. Only writes that end in a newline parse as intended.
Copy at most 31 bytes and terminate behind them.
--- kernel/sched/psi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/sched/psi.c +2 -2
@@ -1566,11 +1566,11 @@ static ssize_t psi_write(struct file *file, const char __user *user_buf,if (!nbytes)return -EINVAL ; - buf_size =min (nbytes,sizeof (buf)); + buf_size =min (nbytes,sizeof (buf) -1 );if (copy_from_user (buf, user_buf, buf_size))return -EFAULT ; - buf[buf_size -1 ] ='\0' ; + buf[buf_size] ='\0' ; seq = file->private_data;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 14/51 proc: sysctl: don't stop readdir at an entry that is being unregistered
In reply to Christian Braunerproc_sys_fill_cache() creates the inode of an entry that has no dentry yet. Since ace0c791e6c3 ("proc/sysctl: Don't grab i_lock under sysctl_lock.") proc_sys_make_inode() refuses to do that once the table is being unregistered and fails with -ENOENT. readdir takes every table it lists into use and unregister_sysctl_table() marks the table as being unregistered before it waits for readdir to let go of it. So an entry that is unregistered right then makes proc_sys_fill_cache() fail, proc_sys_readdir() stops there and returns 0. If that getdents64() call hadn't emitted anything before that entry userspace sees the end of the directory and never gets the entries behind it. A listing of /proc/sys/net/ipv6/conf can end early that way while a device goes away.
So skip an entry whose table is being unregistered, like readdir already skips a link it can't follow. Lookup treats such an entry as gone too since ea5751ccd665 ("proc/sysctl: don't return ENOMEM on lookup when a table is unregistering").
--- fs/proc/proc_sysctl.c | 3 +++ 1 file changed, 3 insertions(+)
fs/proc/proc_sysctl.c +3 -0
@@ -704,6 +704,9 @@ static bool proc_sys_fill_cache(struct file *file,if (unlikely (res)) {dput (child); +/* skip an entry whose table is being unregistered */ +if (res ==ERR_PTR (-ENOENT )) +return true;if (IS_ERR (res))return false;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 15/51 PCI/proc: don't truncate the position of config space accesses
In reply to Christian Braunerproc_bus_pci_write() truncates the file position to an int. A pwrite() at an offset like 0xfffff000 turns it negative, the check against the size of the config space passes and the count it clamps to ends up larger than what was written. The loop then reads past the end of the caller's buffer and writes all of it into config space at negative offsets, which pci_user_write_config_*() pass on to the config accessors unchecked. The write also returns more than it was asked to write.
An offset above 4G that truncates to a small positive value writes to the start of config space instead. proc_bus_pci_read() keeps the position in an unsigned int, so a read at such an offset returns the start of config space instead of hitting EOF.
Check the position against the size of the config space before narrowing it.
--- drivers/pci/proc.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/pci/proc.c +8 -7
@@ -32,8 +32,7 @@ static ssize_t proc_bus_pci_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos) { struct pci_dev *dev = pde_data(file_inode(file)); -unsigned int pos = *ppos; -unsigned int cnt, size; +unsigned int pos, cnt, size;/* * Normal users can read only the standardized portion of the
@@ -51,8 +50,9 @@ static ssize_t proc_bus_pci_read(struct file *file, char __user *buf,if (!nbytes)return 0 ; -if (pos >= size) +if (*ppos >= size)return 0 ; + pos = *ppos;if (nbytes >= size) nbytes = size;if (pos + nbytes > size)
@@ -119,9 +119,8 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf, {struct inode *ino =file_inode (file);struct pci_dev *dev =pde_data (ino); -int pos = *ppos;int size = dev->cfg_size; -int cnt, ret; +int pos, cnt, ret; ret =security_locked_down (LOCKDOWN_PCI_ACCESS );if (ret)
@@ -130,14 +129,16 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,if (!nbytes)return 0 ; +if (*ppos >= size) +return 0 ; + pos = *ppos; +if (resource_is_exclusive (&dev->driver_exclusive_resource, pos, nbytes)) {pci_warn_once (dev,"%s: Unexpected write to kernel-exclusive config offset %x" , current->comm, pos);add_taint (TAINT_USER ,LOCKDEP_STILL_OK ); } -if (pos >= size) -return 0 ;if (nbytes >= size) nbytes = size;if (pos + nbytes > size)
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 16/51 PNP: isapnp: don't read past the end of the buffer on a large pread()
In reply to Christian Braunerisapnp_proc_bus_read() truncates the position to an int. A pread() at an offset like 0xfffffc00 turns it negative, the check against the size of the configuration space passes, and the loop then puts everything from that position up to byte 256 into the reader's buffer, no matter how few bytes were asked for.
Keep the position as a loff_t and clamp the count against what is left of the configuration space.
--- drivers/pnp/isapnp/proc.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/pnp/isapnp/proc.c +2 -4
@@ -23,15 +23,13 @@ static ssize_t isapnp_proc_bus_read(struct file *file, char __user * buf, size_t nbytes, loff_t * ppos) { struct pnp_dev *dev = pde_data(file_inode(file)); -int pos = *ppos; +loff_t pos = *ppos;int cnt, size =256 ;int ret;if (pos >= size)return 0 ; -if (nbytes >= size) - nbytes = size; -if (pos + nbytes > size) +if (nbytes > size - pos) nbytes = size - pos; cnt = nbytes;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 17/51 s390/dasd: don't look in front of the buffer on an empty write
In reply to Christian Braunerdasd_get_user_string() strips a trailing newline by looking at the last byte that was written. For an empty write that's the byte in front of the buffer it just allocated. Only look if something was written.
--- drivers/s390/block/dasd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/s390/block/dasd.c +1 -1
@@ -933,7 +933,7 @@ char *dasd_get_user_string(const char __user *user_buf, size_t user_len)return ERR_PTR (-EFAULT ); }/* got the string, now strip linefeed. */ -if (buffer[user_len -1 ] =='\n' ) +if (user_len && buffer[user_len -1 ] =='\n' ) buffer[user_len -1 ] =0 ;else buffer[user_len] =0 ;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 18/51 platform/x86: toshiba_acpi: reject malformed brightness and fan writes
In reply to Christian BraunerWhen lcd_proc_write() and fan_proc_write() were refactored their checks were inverted but the && stayed. They only fail if sscanf() didn't parse anything and the value is out of range at the same time, and for the brightness value < 0 && value > levels can't be true at all. So an out of range brightness or fan setting is passed on to the hardware, and so is the uninitialized value if nothing could be parsed.
Restore the checks as they were before the refactoring.
--- drivers/platform/x86/toshiba_acpi.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/toshiba_acpi.c +4 -4
@@ -1498,8 +1498,8 @@ static ssize_t lcd_proc_write(struct file *file, const char __user *buf, cmd[len] ='\0' ; levels = dev->backlight_dev->props.max_brightness +1 ; -if (sscanf (cmd," brightness : %i" , &value) !=1 && - value <0 && value > levels) +if (sscanf (cmd," brightness : %i" , &value) !=1 || + value <0 || value >= levels)return -EINVAL ;if (set_lcd_brightness (dev, value))
@@ -1678,8 +1678,8 @@ static ssize_t fan_proc_write(struct file *file, const char __user *buf,return -EFAULT ; cmd[len] ='\0' ; -if (sscanf (cmd," force_on : %i" , &value) !=1 && - value !=0 && value !=1 ) +if (sscanf (cmd," force_on : %i" , &value) !=1 || + value <0 || value >1 )return -EINVAL ;if (set_fan_status (dev, value))
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 19/51 xtensa: simdisk: don't read the file name without the lock
In reply to Christian Braunerproc_read_simdisk() looks at dev->filename without holding dev->lock. A write to the same file detaches the disk and frees the name under the lock, so a concurrent reader can copy out freed memory. Copy the name with the lock held.
--- arch/xtensa/platforms/iss/simdisk.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-)
arch/xtensa/platforms/iss/simdisk.c +12 -13
@@ -238,22 +238,21 @@ static ssize_t proc_read_simdisk(struct file *file, char __user *buf, size_t size, loff_t *ppos) { struct simdisk *dev = pde_data(file_inode(file)); -const char *s = dev->filename; -if (s) { -ssize_t len =strlen (s); -char *temp =kmalloc (len +2 ,GFP_KERNEL ); +ssize_t len; +char *temp; -if (!temp) -return -ENOMEM ; +/* a write sets at most a page, see proc_write_simdisk() */ + temp =kmalloc (PAGE_SIZE +2 ,GFP_KERNEL ); +if (!temp) +return -ENOMEM ; - len =scnprintf (temp, len +2 ,"%s\n" , s); - len =simple_read_from_buffer (buf, size, ppos, - temp, len); +spin_lock (&dev->lock); + len =scnprintf (temp,PAGE_SIZE +2 ,"%s\n" , dev->filename ?:"" ); +spin_unlock (&dev->lock); -kfree (temp); -return len; - } -return simple_read_from_buffer (buf, size, ppos,"\n" ,1 ); + len =simple_read_from_buffer (buf, size, ppos, temp, len); +kfree (temp); +return len; } static ssize_t proc_write_simdisk(struct file *file, const char __user *buf,
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 20/51 powerpc/rtas_flash: don't read past the validate message
In reply to Christian Braunerget_validate_flash_msg() adds up what snprintf() returns, which is the length of the whole string and not what fit into msg. If the update results the firmware hands back don't fit into what is left of the 256 bytes, the length ends up larger than msg and validate_flash_read() copies whatever follows it on the stack to userspace. Use scnprintf().
--- arch/powerpc/kernel/rtas_flash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/rtas_flash.c +1 -1
@@ -448,7 +448,7 @@ static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf, n =sprintf (msg,"%d\n" , args_buf->update_results);if ((args_buf->update_results >=VALIDATE_CUR_UNKNOWN ) || (args_buf->update_results ==VALIDATE_TMP_UPDATE )) - n +=snprintf (msg + n, msglen - n,"%s\n" , + n +=scnprintf (msg + n, msglen - n,"%s\n" , args_buf->buf); }else { n =sprintf (msg,"%d\n" , args_buf->status);
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 21/51 powerpc/rtasd: zero the error log buffer
In reply to Christian Braunerrtas_log_read() hands out a whole slot of the error log but pSeries_log_error() only fills in the sequence number and the event, which is usually a lot shorter. The rest of the slot is whatever vmalloc() left there. Use vzalloc().
--- arch/powerpc/kernel/rtasd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/rtasd.c +1 -1
@@ -523,7 +523,7 @@ static int __init rtas_event_scan_init(void) rtas_error_log_max =rtas_get_error_log_max (); rtas_error_log_buffer_max = rtas_error_log_max +sizeof (int ); - rtas_log_buf =vmalloc (array_size (LOG_NUMBER , + rtas_log_buf =vzalloc (array_size (LOG_NUMBER , rtas_error_log_buffer_max));if (!rtas_log_buf) {printk (KERN_ERR "rtasd: no memory\n" );
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 22/51 ALSA: info: don't write past the end of a data entry
In reply to Christian Braunersnd_info_entry_write() clamps the count to what is left of the entry by computing entry->size - pos as a size_t. For a position past the end that wraps and the write goes to the driver unclamped, for opl4 into sample memory outside of what the entry covers. At the end of the entry the driver gets a count of 0 and vmalloc(0) warns. Before the clamp moved into the common code the driver ignored such writes. Fail them with -ENOSPC instead, like a block device does at the end of the device. Returning 0 would make writers that use stdio retry forever.
--- sound/core/info.c | 2 ++ 1 file changed, 2 insertions(+)
sound/core/info.c +2 -0
@@ -171,6 +171,8 @@ static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer pos = *offset;if (!valid_pos (pos, count))return -EIO ; +if (pos >= entry->size) +return count ? -ENOSPC :0 ;if (count >0 ) {size_t maxsize = entry->size - pos; count =min (count, maxsize);
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 23/51 scsi: BusLogic: don't return 0 from writes to the proc file
In reply to Christian BraunerA write to /proc/scsi/BusLogic/<n> resets the statistics and returns 0. Writers that use stdio, echo for example, take that as no progress and retry forever. Return the number of bytes that were written.
--- drivers/scsi/BusLogic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/BusLogic.c +1 -1
@@ -3328,7 +3328,7 @@ static int blogic_write_info(struct Scsi_Host *shost, char *procbuf, adapter->ext_resets =0 ; adapter->adapter_intern_errors =0 ;memset (tgt_stats,0 ,BLOGIC_MAXDEV *sizeof (struct blogic_tgt_stats )); -return 0 ; +return bytes_avail; }static int blogic_show_info (struct seq_file *m,struct Scsi_Host *shost)
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 24/51 scsi: ips: don't return 0 from writes to the proc file
In reply to Christian Braunerips_write_info() ignores what is written to /proc/scsi/ips/<n> and returns 0. Writers that use stdio, echo for example, take that as no progress and retry forever. Return the number of bytes that were written.
--- drivers/scsi/ips.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/ips.c +1 -1
@@ -1428,7 +1428,7 @@ ips_write_info(struct Scsi_Host *host, char *buffer, int length)if (!ha)return (-EINVAL ); -return 0 ; +return length; } static int
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 25/51 wifi: libipw: don't return 0 from writes to debug_level
In reply to Christian Braunerdebug_level_proc_write() returns the length of the string it parsed. A write that starts with a NUL byte thus returns 0 and writers that use stdio retry it forever. Return the number of bytes that were written instead, which also stops writes longer than the 11 bytes it looks at from coming up short.
--- drivers/net/wireless/intel/ipw2x00/libipw_module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/intel/ipw2x00/libipw_module.c +1 -1
@@ -236,7 +236,7 @@ static ssize_t debug_level_proc_write(struct file *file,else libipw_debug_level = val; -return strnlen (buf, len); +return count; }static const struct proc_ops debug_level_proc_ops = {
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 26/51 scsi: core: NUL-terminate what is written to the host proc files
In reply to Christian Braunerproc_scsi_host_write() copies what is written into a page and hands it to ->write_info() without terminating it. ppa and imm parse it with strncmp() and simple_strtoul(), which read on past what was written into whatever the page held before. Terminate the copy. A write is at most PROC_BLOCK_SIZE bytes, so the page always has room for that.
--- drivers/scsi/scsi_proc.c | 1 + 1 file changed, 1 insertion(+)
drivers/scsi/scsi_proc.c +1 -0
@@ -80,6 +80,7 @@ static ssize_t proc_scsi_host_write(struct file *file, const char __user *buf, ret = -EFAULT ;if (copy_from_user (page, buf, count)) goto out; + page[count] ='\0' ; ret = shost->hostt->write_info (shost, page, count); } out:
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 27/51 um: NUL-terminate what is written to /proc/exitcode
In reply to Christian Braunerexitcode_proc_write() copies up to sizeof(buf) bytes of the write into buf and hands it to simple_strtol() without terminating it. So "echo -n 5" parses whatever the stack held after the '5', and a write of seven bytes or more reads past the end of buf. Terminate the copy.
--- arch/um/kernel/exitcode.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/um/kernel/exitcode.c +2 -1
@@ -43,9 +43,10 @@ static ssize_t exitcode_proc_write(struct file *file,size_t size;int tmp; - size =min (count,sizeof (buf)); + size =min (count,sizeof (buf) -1 );if (copy_from_user (buf, buffer, size))return -EFAULT ; + buf[size] ='\0' ; tmp =simple_strtol (buf, &end,0 );if ((*end !='\0' ) && !isspace (*end))
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 28/51 ntfs3: don't warn when a huge label is written
In reply to Christian Braunerntfs3_label_write() allocates as many bytes as are written with plain kmalloc(). A write that is larger than what kmalloc() can hand out then trips the page allocator's warning, which panics a panic_on_warn kernel. Use memdup_user(), which fails such an allocation quietly with -ENOMEM like before.
--- fs/ntfs3/super.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-)
fs/ntfs3/super.c +3 -8
@@ -576,15 +576,10 @@ static ssize_t ntfs3_label_write(struct file *file, const char __user *buffer,if (sb_rdonly (sb))return -EROFS ; - label =kmalloc (count,GFP_NOFS ); + label =memdup_user (buffer, count); +if (IS_ERR (label)) +return PTR_ERR (label); -if (!label) -return -ENOMEM ; - -if (copy_from_user (label, buffer, ret)) { - ret = -EFAULT ; - goto out; - }while (ret >0 && label[ret -1 ] =='\n' ) ret -=1 ;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 29/51 proc: don't walk through a non-directory when resolving a name
In reply to Christian Brauner__xlate_proc_name() walks the components of a name like "a/b/c" and takes whatever it finds for "a" and "b" as the directory to go on in. It never checks that they actually are directories. So proc_create("file/x") hangs x into the subdir tree of the regular file "file". Nobody can look x up there, but remove_proc_subtree() on the file removes and frees x along with it, behind the back of whoever registered x.
pktgen passes the name the writer of "add_device" picks to proc_create_data(), and only the part in front of the '@' has to name a network device:
echo "add_device lo@" > /proc/net/pktgen/kpktgend_0
echo "add_device lo@/x" > /proc/net/pktgen/kpktgend_1
echo "rem_device_all" > /proc/net/pktgen/kpktgend_0
echo "rem_device_all" > /proc/net/pktgen/kpktgend_1
The third write frees both entries and the fourth one makes kpktgend_1 call proc_remove() on the freed second one:
BUG: KASAN: slab-use-after-free in proc_remove+0x8f/0xa0 Read of size 8 at addr ff110001134c8e70 by task kpktgend_1/243 Call Trace: proc_remove+0x8f/0xa0 pktgen_remove_device.isra.0+0x118/0x500 pktgen_rem_all_ifs+0xeb/0x1a0 pktgen_thread_worker+0x1b42/0x7280
The files of a network namespace belong to the root of its user namespace and these writes don't require a capability. So once pktgen is loaded an unprivileged user can do this in namespaces of its own.
Treat a component that isn't a directory like one that doesn't exist. All names with a '/' that the tree passes in as literals only walk through directories. pktgen is fixed separately to not pass such names in the first place.
--- fs/proc/generic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/proc/generic.c +1 -1
@@ -170,7 +170,7 @@ static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret, de = *ret ?: &proc_root;while ((next =strchr (cp,'/' )) !=NULL ) { de =pde_subdir_find (de, cp, next - cp); -if (!de) { +if (!de || !S_ISDIR (de->mode)) {WARN (1 ,"name '%s'\n" , name);return -ENOENT ; }
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 30/51 net: pktgen: reject device names with a '/'
In reply to Christian Brauner"add_device" takes a name like eth0@1. Only the part in front of the '@' has to name a network device. The rest is free text and all of it becomes the name of a file in /proc/net/pktgen, where procfs takes a '/' as a directory separator. So "add_device lo@a/b" makes procfs look for a directory "lo@a" that doesn't exist and warn about it. Any user can do that in a network namespace of its own:
name 'lo@a/b'
WARNING: fs/proc/generic.c:174 at __xlate_proc_name+0xb1/0xf0, CPU#3: bash/241 Call Trace: __proc_create+0xd0/0x9b0 proc_create_reg+0x45/0xf0 proc_create_data+0x77/0xf0 pktgen_thread_write+0xd8d/0x1750
With panic_on_warn that takes the machine down. And if "lo@a" exists because somebody added a device of that name, procfs used to register the new entry below that regular file, and removing "lo@a" freed both.
Reject names with a '/'.
--- net/core/pktgen.c | 4 ++++ 1 file changed, 4 insertions(+)
net/core/pktgen.c +4 -0
@@ -3831,6 +3831,10 @@ static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)int err;int node =cpu_to_node (t->cpu); +/* The name becomes the name of a file in /proc/net/pktgen. */ +if (strchr (ifname,'/' )) +return -EINVAL ; +/* We don't allow a device to be on several threads */ if (__pktgen_NN_threads (t->net, ifname,FIND )) {
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 31/51 proc: don't WARN about a name that is already registered
In reply to Christian Braunerproc_register() WARNs when the directory already has an entry of the same name and fails the registration. Callers that pick their names themselves should never get there but some take names from userspace. pktgen creates a file for each device a writer of "add_device" adds, named after the device. A device can have any name, including "pgctrl" or "kpktgend_0", the names of pktgen's own files:
ip link set lo name pgctrl
ip link set pgctrl up
echo "add_device pgctrl" > /proc/net/pktgen/kpktgend_0
proc_dir_entry 'pktgen/pgctrl' already registered
WARNING: fs/proc/generic.c:404 at proc_register+0x555/0x8b0, CPU#0: bash/241 Call Trace: proc_create_data+0x9f/0xf0 pktgen_thread_write+0xd9b/0x1780 proc_reg_write+0x1c2/0x3c0 vfs_write+0x1cd/0xfa0
The files of a network namespace belong to the root of its user namespace and none of this requires a capability. So once pktgen is loaded an unprivileged user can taint the kernel, or crash it with panic_on_warn, from namespaces of its own. pktgen itself copes with the failed registration just fine.
So go back to what proc_register() did before commit 6c2f91e077f1 ("proc: use WARN() rather than printk+backtrace") and what sysfs_warn_dup() does for sysfs: report the name with pr_warn() and dump the stack. While at it, do that after dropping proc_subdir_lock instead of printing a backtrace while holding the lock that every lookup and readdir in procfs needs.
--- fs/proc/generic.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
fs/proc/generic.c +3 -2
@@ -402,9 +402,10 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,write_lock (&proc_subdir_lock); dp->parent = dir;if (pde_subdir_insert (dir, dp) == false) { -WARN (1 ,"proc_dir_entry '%s/%s' already registered\n" , - dir->name, dp->name);write_unlock (&proc_subdir_lock); +pr_warn ("proc_dir_entry '%s/%s' already registered\n" , + dir->name, dp->name); +dump_stack (); goto out_free_inum; }write_unlock (&proc_subdir_lock);
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 32/51 proc: resume readdir after the last name it returned
In reply to Christian Braunerproc_readdir_de() counts the entries of a directory and resumes a listing at the number getdents64() stopped at. The entries are sorted by name, so one that is added or removed in between shifts everything behind it by one. Listing /proc/net/dev_snmp6 with a buffer that holds ".", ".." and "lo" and adding a device whose name sorts before "lo" makes the next call return "lo" a second time. Removing one that sorted before the position makes the next call skip an entry instead.
Since commit 895b4c0c79b0 ("fs/proc: fix uaf in proc_readdir_de()") an entry that is removed while it is being emitted also ends the listing right there. pde_subdir_next() finds nothing after an erased entry, so proc_readdir_de() reports the end of the directory and the next call resumes by number, skipping another entry. For /proc itself proc_root_readdir() takes that as the end of the static entries and moves on to the pids, so the rest of the static entries is missing. And if the position is behind the last entry when a call starts proc_readdir_de() returns 0, so /proc then lists no pids at all.
So remember the name of the last entry a call emitted in the file and resume at the first entry after it, like kernfs resumes after the node it stopped at. An entry that was removed while it was emitted is continued after by its name as well. If the position was moved by a seek in between the listing counts from the start like before.
--- fs/proc/generic.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++-------- fs/proc/internal.h | 1 + fs/proc/proc_net.c | 1 + fs/proc/root.c | 1 + 4 files changed, 80 insertions(+), 12 deletions(-)
fs/proc/generic.c +77 -12
@@ -87,6 +87,28 @@ static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,return NULL ; } +/* The first entry of @dir that sorts after @name. */ +static struct proc_dir_entry *pde_subdir_after (struct proc_dir_entry *dir, +const char *name, +unsigned int len) +{ +struct rb_node *node = dir->subdir.rb_node, *after =NULL ; + +while (node) { +struct proc_dir_entry *de =rb_entry (node, + struct proc_dir_entry, + subdir_node); + +if (proc_match (name, de, len) <0 ) { + after = node; + node = node->rb_left; + }else { + node = node->rb_right; + } + } + return rb_entry_safe(after, struct proc_dir_entry, subdir_node); +} +static bool pde_subdir_insert (struct proc_dir_entry *dir,struct proc_dir_entry *de) {
@@ -278,6 +300,40 @@ struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,return proc_lookup_de (dir, dentry,PDE (dir)); } +/* +* Where the next readdir resumes. The entries are counted, so one that is +* added or removed in front of the position would shift what the next call +* returns. The name of the last entry emitted doesn't move. +*/ +struct proc_dir_cursor { +loff_t pos;/* the position behind the entry */ +unsigned int len; +char name[NAME_MAX +1 ]; +}; + +/* If this can't remember the entry the next call counts from the start. */ +static void proc_dir_cursor_set (struct file *file,struct proc_dir_entry *de, +loff_t pos) +{ +struct proc_dir_cursor *cursor = file->private_data; + +if (!cursor) { + cursor =kmalloc (sizeof (*cursor),GFP_KERNEL ); +if (!cursor) +return ; + file->private_data = cursor; + } + cursor->pos = pos; + cursor->len = de->namelen; +memcpy (cursor->name, de->name, de->namelen); +} + +int proc_dir_release (struct inode *inode,struct file *file) +{ +kfree (file->private_data); +return 0 ; +} +/* * This returns non-zero if at EOF, so that the /proc * root directory can use this and check if it should
@@ -290,23 +346,26 @@ struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,int proc_readdir_de (struct file *file,struct dir_context *ctx,struct proc_dir_entry *de) { +struct proc_dir_cursor *cursor = file->private_data; +struct proc_dir_entry *dir = de;int i;if (!dir_emit_dots (file, ctx))return 0 ; - i = ctx->pos -2 ;read_lock (&proc_subdir_lock); - de =pde_subdir_first (de); -for (;;) { -if (!de) { -read_unlock (&proc_subdir_lock); -return 0 ; - } -if (!i) -break ; - de =pde_subdir_next (de); - i--; +if (cursor && cursor->pos == ctx->pos) { +/* Resume after the last entry emitted, wherever it is now. */ + de =pde_subdir_after (dir, cursor->name, cursor->len); + }else { +/* Moved by a seek, count from the start like always. */ + de =pde_subdir_first (dir); +for (i = ctx->pos -2 ; de && i; i--) + de =pde_subdir_next (de); + } +if (!de) { +read_unlock (&proc_subdir_lock); +return 1 ; } do {
@@ -319,8 +378,13 @@ int proc_readdir_de(struct file *file, struct dir_context *ctx,return 0 ; } ctx->pos++; +proc_dir_cursor_set (file, de, ctx->pos);read_lock (&proc_subdir_lock); - next =pde_subdir_next (de); +/* Removed in the meantime, go on after its name. */ +if (RB_EMPTY_NODE (&de->subdir_node)) + next =pde_subdir_after (dir, de->name, de->namelen); +else + next =pde_subdir_next (de);pde_put (de); de = next; }while (de);
@@ -348,6 +412,7 @@ static const struct file_operations proc_dir_operations = { .llseek = generic_file_llseek, .read = generic_read_dir, .iterate_shared = proc_readdir, + .release = proc_dir_release, }; static int proc_net_d_revalidate(struct inode *dir, const struct qstr *name,
fs/proc/internal.h +1 -0
@@ -287,6 +287,7 @@ extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int)struct dentry *proc_lookup_de (struct inode *,struct dentry *,struct proc_dir_entry *);extern int proc_readdir (struct file *,struct dir_context *);int proc_readdir_de (struct file *,struct dir_context *,struct proc_dir_entry *); +int proc_dir_release (struct inode *,struct file *);static inline void pde_get (struct proc_dir_entry *pde) {
fs/proc/proc_net.c +1 -0
@@ -351,6 +351,7 @@ const struct file_operations proc_net_operations = { .llseek = generic_file_llseek, .read = generic_read_dir, .iterate_shared = proc_tgid_net_readdir, + .release = proc_dir_release, };static __net_init int proc_net_ns_init (struct net *net)
fs/proc/root.c +1 -0
@@ -441,6 +441,7 @@ static const struct file_operations proc_root_operations = { .read = generic_read_dir, .iterate_shared = proc_root_readdir, .llseek = generic_file_llseek, + .release = proc_dir_release, };/*
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 33/51 selftests/proc: check a /proc listing while entries come and go
In reply to Christian BraunerList /proc/net/dev_snmp6 of a new network namespace while a tun device comes and goes:
- with a buffer that holds the dots and "lo", add a device whose name sorts first, finish the listing: "lo" is listed once
- with that device added and a buffer that holds the dots and it, remove it, finish the listing: "lo" is still listed
- with that device added, stall getdents64() on a userfaultfd page while it writes the device's entry, remove the device, finish the
listing: "lo" is still listed
All three fail without the fix, the first lists "lo" twice, the other two not at all.
--- tools/testing/selftests/proc/.gitignore | 1 + tools/testing/selftests/proc/Makefile | 1 + tools/testing/selftests/proc/config | 4 + tools/testing/selftests/proc/proc-readdir-churn.c | 240 ++++++++++++++++++++++ 4 files changed, 246 insertions(+)
tools/testing/selftests/proc/.gitignore +1 -0
@@ -8,6 +8,7 @@ /proc-maps-race /proc-multiple-procfs /proc-net-dev-lseek +/proc-readdir-churn /proc-empty-vm /proc-pid-vm /proc-self-map-files-001
tools/testing/selftests/proc/Makefile +1 -0
@@ -11,6 +11,7 @@ TEST_GEN_PROGS += proc-2-is-kthread TEST_GEN_PROGS += proc-loadavg-001 TEST_GEN_PROGS += proc-maps-race TEST_GEN_PROGS += proc-net-dev-lseek +TEST_GEN_PROGS += proc-readdir-churn TEST_GEN_PROGS += proc-empty-vm TEST_GEN_PROGS += proc-pid-vm TEST_GEN_PROGS += proc-self-map-files-001
tools/testing/selftests/proc/config +4 -0
@@ -1 +1,5 @@ CONFIG_PROC_FS=y +CONFIG_NET_NS=y +CONFIG_IPV6=y +CONFIG_TUN=y +CONFIG_USERFAULTFD=y
tools/testing/selftests/proc/proc-readdir-churn.c new file +240 -0
240 lines not on this page · raw message
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 34/51 proc: let proc_remove() remove the entry it was given
In reply to Christian Braunerproc_remove() looks the entry up by its name in its parent and removes whatever it finds there:
remove_proc_subtree(de->name, de->parent);
If the entry was removed already, say along with its parent, that is either nothing or a different entry that got the same name in the meantime, and de->parent may well be gone. Two removals of the same entry are a bug in the caller, but going by the name turns it into removing an entry that belongs to somebody else.
Since commit 895b4c0c79b0 ("fs/proc: fix uaf in proc_readdir_de()") an entry that was unlinked has an empty rb node. So check for that under proc_subdir_lock and otherwise remove the entry itself, with the loop of remove_proc_subtree() moved into a helper both of them use. Nothing can help a caller that passes an entry that has been freed already.
--- fs/proc/generic.c | 78 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 27 deletions(-)
fs/proc/generic.c +51 -27
@@ -818,33 +818,14 @@ void remove_proc_entry(const char *name, struct proc_dir_entry *parent) }EXPORT_SYMBOL (remove_proc_entry); -int remove_proc_subtree (const char *name,struct proc_dir_entry *parent) +/* +* Remove @root, already erased from its parent, and everything below it. +* Called with proc_subdir_lock held for writing, returns with it dropped. +*/ +static int __remove_proc_subtree (struct proc_dir_entry *root) { -struct proc_dir_entry *root =NULL , *de, *next; -const char *fn = name; -unsigned int len; - -write_lock (&proc_subdir_lock); -if (__xlate_proc_name (name, &parent, &fn) !=0 ) { -write_unlock (&proc_subdir_lock); -return -ENOENT ; - } - len =strlen (fn); +struct proc_dir_entry *de = root, *next; - root =pde_subdir_find (parent, fn, len); -if (!root) { -write_unlock (&proc_subdir_lock); -return -ENOENT ; - } -if (unlikely (pde_is_permanent (root))) { -write_unlock (&proc_subdir_lock); -WARN (1 ,"removing permanent /proc entry '%s/%s'" , - root->parent->name, root->name); -return -EINVAL ; - } -pde_erase (root, parent); - - de = root;while (1 ) { next =pde_subdir_first (de);if (next) {
@@ -872,6 +853,34 @@ int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)pde_put (root);return 0 ; } + +int remove_proc_subtree (const char *name,struct proc_dir_entry *parent) +{ +struct proc_dir_entry *root =NULL ; +const char *fn = name; +unsigned int len; + +write_lock (&proc_subdir_lock); +if (__xlate_proc_name (name, &parent, &fn) !=0 ) { +write_unlock (&proc_subdir_lock); +return -ENOENT ; + } + len =strlen (fn); + + root =pde_subdir_find (parent, fn, len); +if (!root) { +write_unlock (&proc_subdir_lock); +return -ENOENT ; + } +if (unlikely (pde_is_permanent (root))) { +write_unlock (&proc_subdir_lock); +WARN (1 ,"removing permanent /proc entry '%s/%s'" , + root->parent->name, root->name); +return -EINVAL ; + } +pde_erase (root, parent); +return __remove_proc_subtree (root); +}EXPORT_SYMBOL (remove_proc_subtree);void *proc_get_parent_data (const struct inode *inode)
@@ -883,8 +892,23 @@ EXPORT_SYMBOL_GPL(proc_get_parent_data);void proc_remove (struct proc_dir_entry *de) { -if (de) -remove_proc_subtree (de->name, de->parent); +if (!de) +return ; + +write_lock (&proc_subdir_lock); +/* Removed already, maybe along with its parent. */ +if (RB_EMPTY_NODE (&de->subdir_node)) { +write_unlock (&proc_subdir_lock); +return ; + } +if (unlikely (pde_is_permanent (de))) { +write_unlock (&proc_subdir_lock); +WARN (1 ,"removing permanent /proc entry '%s/%s'" , + de->parent->name, de->name); +return ; + } +pde_erase (de, de->parent); +__remove_proc_subtree (de); }EXPORT_SYMBOL (proc_remove);
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 35/51 proc: warn about entries left in /proc/net of a dying namespace
In reply to Christian Braunerproc_net_ns_exit() runs after all the other pernet exits and frees net->proc_net right after removing "stat" from it. An entry that some pernet exit forgot to remove is left with a freed ->parent, and so PDE_NET() hands seq_open_net() and single_open_net() a freed struct net when the entry is opened through a file descriptor that someone kept.
There is no such entry in the tree that we know of, but nothing would tell us about one either. So warn about it like remove_proc_entry() does for a directory that isn't empty.
--- fs/proc/proc_net.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/proc/proc_net.c +6 -0
@@ -410,7 +410,13 @@ out: static __net_exit void proc_net_ns_exit(struct net *net) { +struct proc_dir_entry *left; +remove_proc_entry ("stat" , net->proc_net); +/* Whatever is left still points at net->proc_net and so at net. */ + left =rb_entry_safe (rb_first (&net->proc_net->subdir), + struct proc_dir_entry, subdir_node); +WARN (left,"/proc/net: leaking at least '%s'\n" , left->name);pde_free (net->proc_net); }
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 36/51 proc: don't let NOMMU map procfs files
In reply to Christian BraunerEvery procfs file has a ->mmap since commit 786d7e1612f0 ("Fix rmmod/read/write races in /proc entries"), whether the entry has a ->proc_mmap or not. On NOMMU validate_mmap_request() takes that to mean the file can be mapped. procfs has no ->mmap_capabilities so a regular file gets NOMMU_MAP_COPY, and a private mapping never calls ->mmap at all. Instead do_mmap() takes nommu_region_sem for writing and do_mmap_private() reads the file into the copy with kernel_read().
Reading /proc/maps takes nommu_region_sem as well:
mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, open("/proc/maps", O_RDONLY), 0)
do_mmap() // down_write(&nommu_region_sem)
-> do_mmap_private()
-> kernel_read()
-> seq_read_iter()
-> nommu_region_list_start() // down_read()The task hangs for good, holding nommu_region_sem and its own mmap_lock, and every mmap(), munmap() and exit after that hangs behind it. /proc/maps is 0444 so any user can do that.
Give the procfs files a ->mmap_capabilities on NOMMU that allows nothing. That covers every entry and not just /proc/maps, procfs can't know which entry's read takes nommu_region_sem. Mapping a procfs file then fails with -ENODEV, like mapping a file without ->mmap did before 786d7e1612f0. NOMMU never called ->proc_mmap() for a regular file anyway, procfs never offered NOMMU_MAP_DIRECT.
--- fs/proc/inode.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
fs/proc/inode.c +24 -0
@@ -466,6 +466,18 @@ proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,return rv; } +#ifndef CONFIG_MMU +/* +* NOMMU never calls ->proc_mmap() for a regular file. It reads the file into +* a private copy instead, with nommu_region_sem held for writing, which +* reading /proc/maps takes as well. Don't map procfs files at all. +*/ +static unsigned int proc_reg_mmap_capabilities (struct file *file) +{ +return 0 ; +} +#endif +static int proc_reg_open (struct inode *inode,struct file *file) {struct proc_dir_entry *pde =PDE (inode);
@@ -559,6 +571,9 @@ static const struct file_operations proc_reg_file_ops = { .unlocked_ioctl = proc_reg_unlocked_ioctl, .mmap = proc_reg_mmap, .get_unmapped_area = proc_reg_get_unmapped_area, +#ifndef CONFIG_MMU + .mmap_capabilities = proc_reg_mmap_capabilities, +#endif .open = proc_reg_open, .release = proc_reg_release, };
@@ -572,6 +587,9 @@ static const struct file_operations proc_iter_file_ops = { .unlocked_ioctl = proc_reg_unlocked_ioctl, .mmap = proc_reg_mmap, .get_unmapped_area = proc_reg_get_unmapped_area, +#ifndef CONFIG_MMU + .mmap_capabilities = proc_reg_mmap_capabilities, +#endif .open = proc_reg_open, .release = proc_reg_release, };
@@ -586,6 +604,9 @@ static const struct file_operations proc_reg_file_ops_compat = { .compat_ioctl = proc_reg_compat_ioctl, .mmap = proc_reg_mmap, .get_unmapped_area = proc_reg_get_unmapped_area, +#ifndef CONFIG_MMU + .mmap_capabilities = proc_reg_mmap_capabilities, +#endif .open = proc_reg_open, .release = proc_reg_release, };
@@ -600,6 +621,9 @@ static const struct file_operations proc_iter_file_ops_compat = { .compat_ioctl = proc_reg_compat_ioctl, .mmap = proc_reg_mmap, .get_unmapped_area = proc_reg_get_unmapped_area, +#ifndef CONFIG_MMU + .mmap_capabilities = proc_reg_mmap_capabilities, +#endif .open = proc_reg_open, .release = proc_reg_release, };
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 37/51 proc/vmcore: don't let vmcore_cleanup() run twice at once
In reply to Christian Braunervmcore_cleanup() has a single caller, fadump's release_mem_store() on powerpc. That checks fw_dump.dump_active without a lock and calls vmcore_cleanup() without one, and kernfs only serializes the writers of one open file. So two tasks that write to /sys/kernel/fadump/release_mem through different files run vmcore_cleanup() at the same time.
The first one removes /proc/vmcore and waits for its readers in proc_remove(). The second one finds the entry already unlinked, returns from proc_remove() right away and frees vmcore_list, the ELF headers and the device dumps while those readers still walk them. Or both of them free the same lists at once.
Serialize vmcore_cleanup() with a mutex of its own. It can't be vmcore_mutex, removing /proc/vmcore calls release_vmcore() which takes it. The second caller then finds nothing left to free.
--- fs/proc/vmcore.c | 5 +++++ 1 file changed, 5 insertions(+)
fs/proc/vmcore.c +5 -0
@@ -1764,6 +1764,11 @@ fs_initcall(vmcore_init);/* Cleanup function for vmcore module. */ void vmcore_cleanup (void ) { +/* Not vmcore_mutex, removing /proc/vmcore calls release_vmcore(). */ +static DEFINE_MUTEX (vmcore_cleanup_mutex); + +/* Two writers of fadump's release_mem can get here at once. */ +guard (mutex)(&vmcore_cleanup_mutex);if (proc_vmcore) {proc_remove (proc_vmcore); proc_vmcore =NULL ;
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 38/51 powerpc/crash_dump: check ioremap_cache() in copy_oldmem_page()
In reply to Christian Braunercopy_oldmem_page() maps a page of the old kernel that isn't in the linear mapping with ioremap_cache() and copies from it without checking whether the mapping worked. If it didn't, the copy reads from an offset into the NULL page. That oopses unless the copy happens to be a user copy, whose exception table entries turn the fault into a short copy.
Return -ENOMEM like the other architectures do. read_from_oldmem() turns that into -EFAULT like any other short copy.
--- arch/powerpc/kernel/crash_dump.c | 2 ++ 1 file changed, 2 insertions(+)
arch/powerpc/kernel/crash_dump.c +2 -0
@@ -86,6 +86,8 @@ ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn, csize =copy_to_iter (vaddr + offset, csize, iter); }else { vaddr =ioremap_cache (paddr,PAGE_SIZE ); +if (!vaddr) +return -ENOMEM ; csize =copy_to_iter (vaddr + offset, csize, iter);iounmap (vaddr); }
Signature · 1 line
2.53.0
Christian Brauner
maintainerPATCH v2 39/51 powerpc/crash_dump: recover from a machine check in copy_oldmem_page()
In reply to Christian BraunerA capture kernel reads the memory of the crashed kernel, and that memory may well have uncorrectable errors. On powerpc a machine check in the kernel is only recovered from if the instruction that took it has an exception table entry (cf. mce_common_process_ue()). copy_oldmem_page() copies with copy_to_iter(). For a read() of /proc/vmcore that is a user copy, all of whose loads have exception table entries, so the machine check turns into a short copy and read_from_oldmem() returns -EFAULT.
But since commit 4a22fd20379c ("vmcore: convert __read_vmcore to use an iov_iter") /proc/vmcore can be spliced too, and splice(), sendfile() and copy_file_range() copy into kernel pages with memcpy(), which has no exception table entries. A machine check there panics the capture kernel. The same goes for the reads of the ELF headers and notes that vmcore does itself, and those have been memcpy() for as long as copy_oldmem_page() has had its kernel buffer branch, since commit 54c32021eb6f ("[PATCH] powerpc: Add arch-dependent copy_oldmem_page").
Copy with _copy_mc_to_iter() instead. PPC64 selects ARCH_HAS_COPY_MC and there it uses copy_mc_generic() for kernel and user destinations alike, which has the exception table entries. Everywhere else it is _copy_to_iter().
--- arch/powerpc/kernel/crash_dump.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/kernel/crash_dump.c +2 -2
@@ -83,12 +83,12 @@ ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn,if (memblock_is_region_memory (paddr, csize)) { vaddr =__va (paddr); - csize =copy_to_iter (vaddr + offset, csize, iter); + csize =_copy_mc_to_iter (vaddr + offset, csize, iter); }else { vaddr =ioremap_cache (paddr,PAGE_SIZE );if (!vaddr)return -ENOMEM ; - csize =copy_to_iter (vaddr + offset, csize, iter); + csize =_copy_mc_to_iter (vaddr + offset, csize, iter);iounmap (vaddr); }
Signature · 1 line
2.53.0
Christian Brauner
maintainerPlease ignore this. Network dropped halfway through and the umbrella went out under a broken message-id and so this doesn't reference the actual complete series.
I'll resend.
Re: [PATCH v2 29/51] proc: don't walk through a non-directory when resolving a name
In reply to Christian BraunerQuoted 37 lines · Christian Brauner
__xlate_proc_name()walks the components of a name like "a/b/c" and takes whatever it finds for "a" and "b" as the directory to go on in. It never checks that they actually are directories. Soproc_create("file/x")hangs x into the subdir tree of the regular file "file". Nobody can look x up there, butremove_proc_subtree()on the file removes and frees x along with it, behind the back of whoever registered x.pktgen passes the name the writer of "
add_device" picks toproc_create_data(), and only the part in front of the '@' has to name a network device:echo "
add_devicelo@" >/proc/net/pktgen/kpktgend_0
echo "add_devicelo@/x" >/proc/net/pktgen/kpktgend_1
echo "rem_device_all" >/proc/net/pktgen/kpktgend_0
echo "rem_device_all" >/proc/net/pktgen/kpktgend_1The third write frees both entries and the fourth one makes
kpktgend_1callproc_remove()on the freed second one:BUG: KASAN: slab-use-after-free in proc_remove+0x8f/0xa0 Read of size 8 at addr ff110001134c8e70 by task kpktgend_1/243 Call Trace: proc_remove+0x8f/0xa0 pktgen_remove_device.isra.0+0x118/0x500 pktgen_rem_all_ifs+0xeb/0x1a0 pktgen_thread_worker+0x1b42/0x7280The files of a network namespace belong to the root of its user namespace and these writes don't require a capability. So once pktgen is loaded an unprivileged user can do this in namespaces of its own.
Treat a component that isn't a directory like one that doesn't exist. All names with a '/' that the tree passes in as literals only walk through directories. pktgen is fixed separately to not pass such names in the first place.
Quoted 4 lines
Reported-by Chris Mason <[email protected]>Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 21 lines
--- fs/proc/generic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/proc/generic.c +1
-1@@ -170,7 +170,7 @@ static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret, de = *ret ?: &proc_root;while ((next =strchr (cp,'/' )) !=NULL ) { de =pde_subdir_find (de, cp, next - cp); -if (!de) { +if (!de || !S_ISDIR (de->mode)) {WARN (1 ,"name '%s'\n" , name);return -ENOENT ; }Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH v2 07/51] lockd: don't hand out the terminator of nlm_end_grace
In reply to Christian BraunerQuoted 6 lines · Christian Brauner
nlm_end_grace_read()answers with "Y\n" or "N\n" from a three byte buffer and hands out all three bytes, the terminating NUL included. Leave the NUL out.Fixes d68e3c4aa416 ("lockd: add a /proc/fs/lockd/nlm_end_grace file")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Makes sense. Feel free to add:
Honza
Quoted 21 lines
--- fs/lockd/procfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/lockd/procfs.c +1
-1@@ -57,7 +57,7 @@ nlm_end_grace_read(struct file *file, char __user *buf, size_t size, resp[1 ] ='\n' ; resp[2 ] ='\0' ; -return simple_read_from_buffer (buf, size, pos, resp,sizeof (resp)); +return simple_read_from_buffer (buf, size, pos, resp,sizeof (resp) -1 ); }static const struct proc_ops lockd_end_grace_proc_ops = {Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH v2 14/51] proc: sysctl: don't stop readdir at an entry that is being unregistered
In reply to Christian BraunerQuoted 13 lines · Christian Brauner
proc_sys_fill_cache()creates the inode of an entry that has no dentry yet. Sinceace0c791e6c3("proc/sysctl: Don't grabi_lockundersysctl_lock.")proc_sys_make_inode()refuses to do that once the table is being unregistered and fails with-ENOENT. readdir takes every table it lists into use andunregister_sysctl_table()marks the table as being unregistered before it waits for readdir to let go of it. So an entry that is unregistered right then makesproc_sys_fill_cache()fail,proc_sys_readdir()stops there and returns 0. If thatgetdents64()call hadn't emitted anything before that entry userspace sees the end of the directory and never gets the entries behind it. A listing of/proc/sys/net/ipv6/confcan end early that way while a device goes away.
Quoted 8 lines
So skip an entry whose table is being unregistered, like readdir already skips a link it can't follow. Lookup treats such an entry as gone too since
ea5751ccd665("proc/sysctl: don't return ENOMEM on lookup when a table is unregistering").Fixes ace0c791e6c3 ("proc/sysctl: Don't grab i_lock under sysctl_lock.")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks sane. Feel free to add:
Honza
Quoted 22 lines
--- fs/proc/proc_sysctl.c | 3 +++ 1 file changed, 3 insertions(+)fs/proc/proc_sysctl.c +3
-0@@ -704,6 +704,9 @@ static bool proc_sys_fill_cache(struct file *file,if (unlikely (res)) {dput (child); +/* skip an entry whose table is being unregistered */ +if (res ==ERR_PTR (-ENOENT )) +return true;if (IS_ERR (res))return false;Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH v2 31/51] proc: don't WARN about a name that is already registered
In reply to Christian BraunerQuoted 32 lines · Christian Brauner
proc_register()WARNs when the directory already has an entry of the same name and fails the registration. Callers that pick their names themselves should never get there but some take names from userspace. pktgen creates a file for each device a writer of "add_device" adds, named after the device. A device can have any name, including "pgctrl" or "kpktgend_0", the names of pktgen's own files:ip link set lo name pgctrl
ip link set pgctrl up
echo "add_devicepgctrl" >/proc/net/pktgen/kpktgend_0
proc_dir_entry'pktgen/pgctrl' already registeredWARNING: fs/proc/generic.c:404 at proc_register+0x555/0x8b0, CPU#0: bash/241 Call Trace: proc_create_data+0x9f/0xf0 pktgen_thread_write+0xd9b/0x1780 proc_reg_write+0x1c2/0x3c0 vfs_write+0x1cd/0xfa0The files of a network namespace belong to the root of its user namespace and none of this requires a capability. So once pktgen is loaded an unprivileged user can taint the kernel, or crash it with
panic_on_warn, from namespaces of its own. pktgen itself copes with the failed registration just fine.So go back to what
proc_register()did before commit6c2f91e077f1("proc: useWARN()rather than printk+backtrace") and whatsysfs_warn_dup()does for sysfs: report the name withpr_warn()and dump the stack. While at it, do that after droppingproc_subdir_lockinstead of printing a backtrace while holding the lock that every lookup and readdir in procfs needs.
Quoted 4 lines
Reported-by Chris Mason <[email protected]>Fixes 6c2f91e077f1 ("proc: use WARN() rather than printk+backtrace")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Fair. Feel free to add:
Honza
Quoted 25 lines
--- fs/proc/generic.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)fs/proc/generic.c +3
-2@@ -402,9 +402,10 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,write_lock (&proc_subdir_lock); dp->parent = dir;if (pde_subdir_insert (dir, dp) == false) { -WARN (1 ,"proc_dir_entry '%s/%s' already registered\n" , - dir->name, dp->name);write_unlock (&proc_subdir_lock); +pr_warn ("proc_dir_entry '%s/%s' already registered\n" , + dir->name, dp->name); +dump_stack (); goto out_free_inum; }write_unlock (&proc_subdir_lock);Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH v2 32/51] proc: resume readdir after the last name it returned
In reply to Christian BraunerQuoted 24 lines · Christian Brauner
proc_readdir_de()counts the entries of a directory and resumes a listing at the numbergetdents64()stopped at. The entries are sorted by name, so one that is added or removed in between shifts everything behind it by one. Listing/proc/net/dev_snmp6with a buffer that holds ".", ".." and "lo" and adding a device whose name sorts before "lo" makes the next call return "lo" a second time. Removing one that sorted before the position makes the next call skip an entry instead.Since commit
895b4c0c79b0("fs/proc: fix uaf inproc_readdir_de()") an entry that is removed while it is being emitted also ends the listing right there.pde_subdir_next()finds nothing after an erased entry, soproc_readdir_de()reports the end of the directory and the next call resumes by number, skipping another entry. For /proc itselfproc_root_readdir()takes that as the end of the static entries and moves on to the pids, so the rest of the static entries is missing. And if the position is behind the last entry when a call startsproc_readdir_de()returns 0, so /proc then lists no pids at all.So remember the name of the last entry a call emitted in the file and resume at the first entry after it, like kernfs resumes after the node it stopped at. An entry that was removed while it was emitted is continued after by its name as well. If the position was moved by a seek in between the listing counts from the start like before.
Quoted 4 lines
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Fixes 895b4c0c79b0 ("fs/proc: fix uaf in proc_readdir_de()")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks good to me. Feel free to add:
Honza
Quoted 182 lines
--- fs/proc/generic.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++-------- fs/proc/internal.h | 1 + fs/proc/proc_net.c | 1 + fs/proc/root.c | 1 + 4 files changed, 80 insertions(+), 12 deletions(-)fs/proc/generic.c +77
-12@@ -87,6 +87,28 @@ static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,return NULL ; } +/* The first entry of @dir that sorts after @name. */ +static struct proc_dir_entry *pde_subdir_after (struct proc_dir_entry *dir, +const char *name, +unsigned int len) +{ +struct rb_node *node = dir->subdir.rb_node, *after =NULL ; + +while (node) { +struct proc_dir_entry *de =rb_entry (node, + struct proc_dir_entry, + subdir_node); + +if (proc_match (name, de, len) <0 ) { + after = node; + node = node->rb_left; + }else { + node = node->rb_right; + } + } + return rb_entry_safe(after, struct proc_dir_entry, subdir_node); +} +static bool pde_subdir_insert (struct proc_dir_entry *dir,struct proc_dir_entry *de) {@@ -278,6 +300,40 @@ struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,return proc_lookup_de (dir, dentry,PDE (dir)); } +/* +* Where the next readdir resumes. The entries are counted, so one that is +* added or removed in front of the position would shift what the next call +* returns. The name of the last entry emitted doesn't move. +*/ +struct proc_dir_cursor { +loff_t pos;/* the position behind the entry */ +unsigned int len; +char name[NAME_MAX +1 ]; +}; + +/* If this can't remember the entry the next call counts from the start. */ +static void proc_dir_cursor_set (struct file *file,struct proc_dir_entry *de, +loff_t pos) +{ +struct proc_dir_cursor *cursor = file->private_data; + +if (!cursor) { + cursor =kmalloc (sizeof (*cursor),GFP_KERNEL ); +if (!cursor) +return ; + file->private_data = cursor; + } + cursor->pos = pos; + cursor->len = de->namelen; +memcpy (cursor->name, de->name, de->namelen); +} + +int proc_dir_release (struct inode *inode,struct file *file) +{ +kfree (file->private_data); +return 0 ; +} +/* * This returns non-zero if at EOF, so that the /proc * root directory can use this and check if it should @@ -290,23 +346,26 @@ struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,int proc_readdir_de (struct file *file,struct dir_context *ctx,struct proc_dir_entry *de) { +struct proc_dir_cursor *cursor = file->private_data; +struct proc_dir_entry *dir = de;int i;if (!dir_emit_dots (file, ctx))return 0 ; - i = ctx->pos -2 ;read_lock (&proc_subdir_lock); - de =pde_subdir_first (de); -for (;;) { -if (!de) { -read_unlock (&proc_subdir_lock); -return 0 ; - } -if (!i) -break ; - de =pde_subdir_next (de); - i--; +if (cursor && cursor->pos == ctx->pos) { +/* Resume after the last entry emitted, wherever it is now. */ + de =pde_subdir_after (dir, cursor->name, cursor->len); + }else { +/* Moved by a seek, count from the start like always. */ + de =pde_subdir_first (dir); +for (i = ctx->pos -2 ; de && i; i--) + de =pde_subdir_next (de); + } +if (!de) { +read_unlock (&proc_subdir_lock); +return 1 ; } do {@@ -319,8 +378,13 @@ int proc_readdir_de(struct file *file, struct dir_context *ctx,return 0 ; } ctx->pos++; +proc_dir_cursor_set (file, de, ctx->pos);read_lock (&proc_subdir_lock); - next =pde_subdir_next (de); +/* Removed in the meantime, go on after its name. */ +if (RB_EMPTY_NODE (&de->subdir_node)) + next =pde_subdir_after (dir, de->name, de->namelen); +else + next =pde_subdir_next (de);pde_put (de); de = next; }while (de);@@ -348,6 +412,7 @@ static const struct file_operations proc_dir_operations = { .llseek = generic_file_llseek, .read = generic_read_dir, .iterate_shared = proc_readdir, + .release = proc_dir_release, }; static int proc_net_d_revalidate(struct inode *dir, const struct qstr *name,fs/proc/internal.h +1
-0@@ -287,6 +287,7 @@ extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int)struct dentry *proc_lookup_de (struct inode *,struct dentry *,struct proc_dir_entry *);extern int proc_readdir (struct file *,struct dir_context *);int proc_readdir_de (struct file *,struct dir_context *,struct proc_dir_entry *); +int proc_dir_release (struct inode *,struct file *);static inline void pde_get (struct proc_dir_entry *pde) {fs/proc/proc_net.c +1
-0@@ -351,6 +351,7 @@ const struct file_operations proc_net_operations = { .llseek = generic_file_llseek, .read = generic_read_dir, .iterate_shared = proc_tgid_net_readdir, + .release = proc_dir_release, };static __net_init int proc_net_ns_init (struct net *net)fs/proc/root.c +1
-0@@ -441,6 +441,7 @@ static const struct file_operations proc_root_operations = { .read = generic_read_dir, .iterate_shared = proc_root_readdir, .llseek = generic_file_llseek, + .release = proc_dir_release, };/* Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH v2 34/51] proc: let proc_remove() remove the entry it was given
In reply to Christian BraunerQuoted 18 lines · Christian Brauner
proc_remove()looks the entry up by its name in its parent and removes whatever it finds there:remove_proc_subtree(de->name, de->parent);If the entry was removed already, say along with its parent, that is either nothing or a different entry that got the same name in the meantime, and
de->parentmay well be gone. Two removals of the same entry are a bug in the caller, but going by the name turns it into removing an entry that belongs to somebody else.Since commit
895b4c0c79b0("fs/proc: fix uaf inproc_readdir_de()") an entry that was unlinked has an empty rb node. So check for that underproc_subdir_lockand otherwise remove the entry itself, with the loop ofremove_proc_subtree()moved into a helper both of them use. Nothing can help a caller that passes an entry that has been freed already.
Quoted 3 lines
Reported-by Chris Mason <[email protected]>Fixes a8ca16ea7b0a ("proc: Supply a function to remove a proc entry by PDE")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 113 lines
--- fs/proc/generic.c | 78 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 27 deletions(-)fs/proc/generic.c +51
-27@@ -818,33 +818,14 @@ void remove_proc_entry(const char *name, struct proc_dir_entry *parent) }EXPORT_SYMBOL (remove_proc_entry); -int remove_proc_subtree (const char *name,struct proc_dir_entry *parent) +/* +* Remove @root, already erased from its parent, and everything below it. +* Called with proc_subdir_lock held for writing, returns with it dropped. +*/ +static int __remove_proc_subtree (struct proc_dir_entry *root) { -struct proc_dir_entry *root =NULL , *de, *next; -const char *fn = name; -unsigned int len; - -write_lock (&proc_subdir_lock); -if (__xlate_proc_name (name, &parent, &fn) !=0 ) { -write_unlock (&proc_subdir_lock); -return -ENOENT ; - } - len =strlen (fn); +struct proc_dir_entry *de = root, *next; - root =pde_subdir_find (parent, fn, len); -if (!root) { -write_unlock (&proc_subdir_lock); -return -ENOENT ; - } -if (unlikely (pde_is_permanent (root))) { -write_unlock (&proc_subdir_lock); -WARN (1 ,"removing permanent /proc entry '%s/%s'" , - root->parent->name, root->name); -return -EINVAL ; - } -pde_erase (root, parent); - - de = root;while (1 ) { next =pde_subdir_first (de);if (next) {@@ -872,6 +853,34 @@ int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)pde_put (root);return 0 ; } + +int remove_proc_subtree (const char *name,struct proc_dir_entry *parent) +{ +struct proc_dir_entry *root =NULL ; +const char *fn = name; +unsigned int len; + +write_lock (&proc_subdir_lock); +if (__xlate_proc_name (name, &parent, &fn) !=0 ) { +write_unlock (&proc_subdir_lock); +return -ENOENT ; + } + len =strlen (fn); + + root =pde_subdir_find (parent, fn, len); +if (!root) { +write_unlock (&proc_subdir_lock); +return -ENOENT ; + } +if (unlikely (pde_is_permanent (root))) { +write_unlock (&proc_subdir_lock); +WARN (1 ,"removing permanent /proc entry '%s/%s'" , + root->parent->name, root->name); +return -EINVAL ; + } +pde_erase (root, parent); +return __remove_proc_subtree (root); +}EXPORT_SYMBOL (remove_proc_subtree);void *proc_get_parent_data (const struct inode *inode)@@ -883,8 +892,23 @@ EXPORT_SYMBOL_GPL(proc_get_parent_data);void proc_remove (struct proc_dir_entry *de) { -if (de) -remove_proc_subtree (de->name, de->parent); +if (!de) +return ; + +write_lock (&proc_subdir_lock); +/* Removed already, maybe along with its parent. */ +if (RB_EMPTY_NODE (&de->subdir_node)) { +write_unlock (&proc_subdir_lock); +return ; + } +if (unlikely (pde_is_permanent (de))) { +write_unlock (&proc_subdir_lock); +WARN (1 ,"removing permanent /proc entry '%s/%s'" , + de->parent->name, de->name); +return ; + } +pde_erase (de, de->parent); +__remove_proc_subtree (de); }EXPORT_SYMBOL (proc_remove);Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH v2 35/51] proc: warn about entries left in /proc/net of a dying namespace
In reply to Christian BraunerQuoted 6 lines · Christian Brauner
proc_net_ns_exit()runs after all the other pernet exits and freesnet->proc_netright after removing "stat" from it. An entry that some pernet exit forgot to remove is left with a freed ->parent, and soPDE_NET()handsseq_open_net()andsingle_open_net()a freed struct net when the entry is opened through a file descriptor that someone kept.
Quoted 7 lines
There is no such entry in the tree that we know of, but nothing would tell us about one either. So warn about it like
remove_proc_entry()does for a directory that isn't empty.Reported-by Chris Mason <[email protected]>Fixes e9720acd728a ("[NET]: Make /proc/net a symlink on /proc/self/net (v3)")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Makes sense. Feel free to add:
Honza
Quoted 26 lines
--- fs/proc/proc_net.c | 6 ++++++ 1 file changed, 6 insertions(+)fs/proc/proc_net.c +6
-0@@ -410,7 +410,13 @@ out: static __net_exit void proc_net_ns_exit(struct net *net) { +struct proc_dir_entry *left; +remove_proc_entry ("stat" , net->proc_net); +/* Whatever is left still points at net->proc_net and so at net. */ + left =rb_entry_safe (rb_first (&net->proc_net->subdir), + struct proc_dir_entry, subdir_node); +WARN (left,"/proc/net: leaking at least '%s'\n" , left->name);pde_free (net->proc_net); }Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH v2 36/51] proc: don't let NOMMU map procfs files
In reply to Christian BraunerQuoted 29 lines · Christian Brauner
Every procfs file has a ->mmap since commit
786d7e1612f0("Fix rmmod/read/write races in /proc entries"), whether the entry has a ->proc_mmap or not. On NOMMUvalidate_mmap_request()takes that to mean the file can be mapped. procfs has no ->mmap_capabilities so a regular file getsNOMMU_MAP_COPY, and a private mapping never calls ->mmap at all. Insteaddo_mmap()takesnommu_region_semfor writing anddo_mmap_private()reads the file into the copy withkernel_read().Reading
/proc/mapstakesnommu_region_semas well:
mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, open("/proc/maps", O_RDONLY), 0)
do_mmap()//down_write(&nommu_region_sem)
->do_mmap_private()-> kernel_read() -> seq_read_iter() -> nommu_region_list_start() // down_read()The task hangs for good, holding
nommu_region_semand its ownmmap_lock, and everymmap(),munmap()and exit after that hangs behind it./proc/mapsis 0444 so any user can do that.Give the procfs files a ->mmap_capabilities on NOMMU that allows nothing. That covers every entry and not just
/proc/maps, procfs can't know which entry's read takesnommu_region_sem. Mapping a procfs file then fails with-ENODEV, like mapping a file without ->mmap did before786d7e1612f0. NOMMU never called ->proc_mmap() for a regular file anyway, procfs never offeredNOMMU_MAP_DIRECT.
Quoted 4 lines
Reported-by Chris Mason <[email protected]>Fixes 786d7e1612f0 ("Fix rmmod/read/write races in /proc entries")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 71 lines
--- fs/proc/inode.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)fs/proc/inode.c +24
-0@@ -466,6 +466,18 @@ proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,return rv; } +#ifndef CONFIG_MMU +/* +* NOMMU never calls ->proc_mmap() for a regular file. It reads the file into +* a private copy instead, with nommu_region_sem held for writing, which +* reading /proc/maps takes as well. Don't map procfs files at all. +*/ +static unsigned int proc_reg_mmap_capabilities (struct file *file) +{ +return 0 ; +} +#endif +static int proc_reg_open (struct inode *inode,struct file *file) {struct proc_dir_entry *pde =PDE (inode);@@ -559,6 +571,9 @@ static const struct file_operations proc_reg_file_ops = { .unlocked_ioctl = proc_reg_unlocked_ioctl, .mmap = proc_reg_mmap, .get_unmapped_area = proc_reg_get_unmapped_area, +#ifndef CONFIG_MMU + .mmap_capabilities = proc_reg_mmap_capabilities, +#endif .open = proc_reg_open, .release = proc_reg_release, };@@ -572,6 +587,9 @@ static const struct file_operations proc_iter_file_ops = { .unlocked_ioctl = proc_reg_unlocked_ioctl, .mmap = proc_reg_mmap, .get_unmapped_area = proc_reg_get_unmapped_area, +#ifndef CONFIG_MMU + .mmap_capabilities = proc_reg_mmap_capabilities, +#endif .open = proc_reg_open, .release = proc_reg_release, };@@ -586,6 +604,9 @@ static const struct file_operations proc_reg_file_ops_compat = { .compat_ioctl = proc_reg_compat_ioctl, .mmap = proc_reg_mmap, .get_unmapped_area = proc_reg_get_unmapped_area, +#ifndef CONFIG_MMU + .mmap_capabilities = proc_reg_mmap_capabilities, +#endif .open = proc_reg_open, .release = proc_reg_release, };@@ -600,6 +621,9 @@ static const struct file_operations proc_iter_file_ops_compat = { .compat_ioctl = proc_reg_compat_ioctl, .mmap = proc_reg_mmap, .get_unmapped_area = proc_reg_get_unmapped_area, +#ifndef CONFIG_MMU + .mmap_capabilities = proc_reg_mmap_capabilities, +#endif .open = proc_reg_open, .release = proc_reg_release, };Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH v2 37/51] proc/vmcore: don't let vmcore_cleanup() run twice at once
In reply to Christian BraunerQuoted 13 lines · Christian Brauner
vmcore_cleanup()has a single caller, fadump'srelease_mem_store()on powerpc. That checks fw_dump.dump_active without a lock and callsvmcore_cleanup()without one, and kernfs only serializes the writers of one open file. So two tasks that write to/sys/kernel/fadump/release_memthrough different files runvmcore_cleanup()at the same time.The first one removes
/proc/vmcoreand waits for its readers inproc_remove(). The second one finds the entry already unlinked, returns fromproc_remove()right away and freesvmcore_list, the ELF headers and the device dumps while those readers still walk them. Or both of them free the same lists at once.
Quoted 8 lines
Serialize
vmcore_cleanup()with a mutex of its own. It can't bevmcore_mutex, removing/proc/vmcorecallsrelease_vmcore()which takes it. The second caller then finds nothing left to free.Reported-by Chris Mason <[email protected]>Fixes b500afff11f6 ("fadump: Invalidate registration and release reserved memory for general use.")Signed-off-by Christian Brauner (Amutable) <[email protected]>
A bit sad but OK. Feel free to add:
Honza
Quoted 24 lines
--- fs/proc/vmcore.c | 5 +++++ 1 file changed, 5 insertions(+)fs/proc/vmcore.c +5
-0@@ -1764,6 +1764,11 @@ fs_initcall(vmcore_init);/* Cleanup function for vmcore module. */ void vmcore_cleanup (void ) { +/* Not vmcore_mutex, removing /proc/vmcore calls release_vmcore(). */ +static DEFINE_MUTEX (vmcore_cleanup_mutex); + +/* Two writers of fadump's release_mem can get here at once. */ +guard (mutex)(&vmcore_cleanup_mutex);if (proc_vmcore) {proc_remove (proc_vmcore); proc_vmcore =NULL ;Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR