Revision bbcd53c960713507ae764bf81970651b5577b95a authored by David Hildenbrand on 07 May 2021, 01:05:55 UTC, committed by Linus Torvalds on 07 May 2021, 07:26:34 UTC
Patch series "drivers/char: remove /dev/kmem for good".

Exploring /dev/kmem and /dev/mem in the context of memory hot(un)plug and
memory ballooning, I started questioning the existence of /dev/kmem.

Comparing it with the /proc/kcore implementation, it does not seem to be
able to deal with things like

a) Pages unmapped from the direct mapping (e.g., to be used by secretmem)
  -> kern_addr_valid(). virt_addr_valid() is not sufficient.

b) Special cases like gart aperture memory that is not to be touched
  -> mem_pfn_is_ram()

Unless I am missing something, it's at least broken in some cases and might
fault/crash the machine.

Looks like its existence has been questioned before in 2005 and 2010 [1],
after ~11 additional years, it might make sense to revive the discussion.

CONFIG_DEVKMEM is only enabled in a single defconfig (on purpose or by
mistake?).  All distributions disable it: in Ubuntu it has been disabled
for more than 10 years, in Debian since 2.6.31, in Fedora at least
starting with FC3, in RHEL starting with RHEL4, in SUSE starting from
15sp2, and OpenSUSE has it disabled as well.

1) /dev/kmem was popular for rootkits [2] before it got disabled
   basically everywhere. Ubuntu documents [3] "There is no modern user of
   /dev/kmem any more beyond attackers using it to load kernel rootkits.".
   RHEL documents in a BZ [5] "it served no practical purpose other than to
   serve as a potential security problem or to enable binary module drivers
   to access structures/functions they shouldn't be touching"

2) /proc/kcore is a decent interface to have a controlled way to read
   kernel memory for debugging puposes. (will need some extensions to
   deal with memory offlining/unplug, memory ballooning, and poisoned
   pages, though)

3) It might be useful for corner case debugging [1]. KDB/KGDB might be a
   better fit, especially, to write random memory; harder to shoot
   yourself into the foot.

4) "Kernel Memory Editor" [4] hasn't seen any updates since 2000 and seems
   to be incompatible with 64bit [1]. For educational purposes,
   /proc/kcore might be used to monitor value updates -- or older
   kernels can be used.

5) It's broken on arm64, and therefore, completely disabled there.

Looks like it's essentially unused and has been replaced by better
suited interfaces for individual tasks (/proc/kcore, KDB/KGDB). Let's
just remove it.

[1] https://lwn.net/Articles/147901/
[2] https://www.linuxjournal.com/article/10505
[3] https://wiki.ubuntu.com/Security/Features#A.2Fdev.2Fkmem_disabled
[4] https://sourceforge.net/projects/kme/
[5] https://bugzilla.redhat.com/show_bug.cgi?id=154796

Link: https://lkml.kernel.org/r/20210324102351.6932-1-david@redhat.com
Link: https://lkml.kernel.org/r/20210324102351.6932-2-david@redhat.com
Signed-off-by: David Hildenbrand <david@redhat.com>
Acked-by: Michal Hocko <mhocko@suse.com>
Acked-by: Kees Cook <keescook@chromium.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Alexander A. Klimov" <grandmaster@al2klimov.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
Cc: Andrew Lunn <andrew@lunn.ch>
Cc: Andrey Zhizhikin <andrey.zhizhikin@leica-geosystems.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Brian Cain <bcain@codeaurora.org>
Cc: Christian Borntraeger <borntraeger@de.ibm.com>
Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: Chris Zankel <chris@zankel.net>
Cc: Corentin Labbe <clabbe@baylibre.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Gerald Schaefer <gerald.schaefer@linux.ibm.com>
Cc: Greentime Hu <green.hu@gmail.com>
Cc: Gregory Clement <gregory.clement@bootlin.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Helge Deller <deller@gmx.de>
Cc: Hillf Danton <hdanton@sina.com>
Cc: huang ying <huang.ying.caritas@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
Cc: "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>
Cc: James Troup <james.troup@canonical.com>
Cc: Jiaxun Yang <jiaxun.yang@flygoat.com>
Cc: Jonas Bonn <jonas@southpole.se>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Kairui Song <kasong@redhat.com>
Cc: Krzysztof Kozlowski <krzk@kernel.org>
Cc: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
Cc: Liviu Dudau <liviu.dudau@arm.com>
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Cc: Luis Chamberlain <mcgrof@kernel.org>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Matt Turner <mattst88@gmail.com>
Cc: Max Filippov <jcmvbkbc@gmail.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Mikulas Patocka <mpatocka@redhat.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Niklas Schnelle <schnelle@linux.ibm.com>
Cc: Oleksiy Avramchenko <oleksiy.avramchenko@sonymobile.com>
Cc: openrisc@lists.librecores.org
Cc: Palmer Dabbelt <palmerdabbelt@google.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: "Pavel Machek (CIP)" <pavel@denx.de>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: "Peter Zijlstra (Intel)" <peterz@infradead.org>
Cc: Pierre Morel <pmorel@linux.ibm.com>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Richard Henderson <rth@twiddle.net>
Cc: Rich Felker <dalias@libc.org>
Cc: Robert Richter <rric@kernel.org>
Cc: Rob Herring <robh@kernel.org>
Cc: Russell King <linux@armlinux.org.uk>
Cc: Sam Ravnborg <sam@ravnborg.org>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
Cc: sparclinux@vger.kernel.org
Cc: Stafford Horne <shorne@gmail.com>
Cc: Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Sudeep Holla <sudeep.holla@arm.com>
Cc: Theodore Dubois <tblodt@icloud.com>
Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Viresh Kumar <viresh.kumar@linaro.org>
Cc: William Cohen <wcohen@redhat.com>
Cc: Xiaoming Ni <nixiaoming@huawei.com>
Cc: Yoshinori Sato <ysato@users.sourceforge.jp>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent cb152a1
Raw File
get_led_device_info.sh
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0

led_common_defs_path="include/dt-bindings/leds/common.h"

num_args=$#
if [ $num_args -eq 1 ]; then
        linux_top=$(dirname `realpath $0` | awk -F/     \
                        '{                              \
                                i=1;                    \
                                while (i <= NF - 2) {   \
                                        printf $i"/";   \
                                        i++;            \
                                };                      \
                        }')
	led_defs_path=$linux_top/$led_common_defs_path
elif [ $num_args -eq 2 ]; then
        led_defs_path=`realpath $2`
else
	echo "Usage: get_led_device_info.sh LED_CDEV_PATH [LED_COMMON_DEFS_PATH]"
	exit 1
fi

if [ ! -f $led_defs_path ]; then
	echo "$led_defs_path doesn't exist"
	exit 1
fi

led_cdev_path=`echo $1 | sed s'/\/$//'`

ls "$led_cdev_path/brightness" > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "Device \"$led_cdev_path\" does not exist."
	exit 1
fi

bus=`readlink $led_cdev_path/device/subsystem | sed s'/.*\///'`
usb_subdev=`readlink $led_cdev_path | grep usb | sed s'/\(.*usb[0-9]*\/[0-9]*-[0-9]*\)\/.*/\1/'`
ls "$led_cdev_path/device/of_node/compatible" > /dev/null 2>&1
of_node_missing=$?

if [ "$bus" = "input" ]; then
	input_node=`readlink $led_cdev_path/device | sed s'/.*\///'`
	if [ ! -z "$usb_subdev" ]; then
		bus="usb"
	fi
fi

if [ "$bus" = "usb" ]; then
	usb_interface=`readlink $led_cdev_path | sed s'/.*\(usb[0-9]*\)/\1/' | cut -d\/ -f3`
	cd $led_cdev_path/../$usb_subdev
	driver=`readlink $usb_interface/driver | sed s'/.*\///'`
	if [ -d "$usb_interface/ieee80211" ]; then
		wifi_phy=`ls -l $usb_interface/ieee80211 | grep phy | awk '{print $9}'`
	fi
	idVendor=`cat idVendor`
	idProduct=`cat idProduct`
	manufacturer=`cat manufacturer`
	product=`cat product`
elif [ "$bus" = "input" ]; then
	cd $led_cdev_path
	product=`cat device/name`
	driver=`cat device/device/driver/description`
elif [ $of_node_missing -eq 0 ]; then
	cd $led_cdev_path
	compatible=`cat device/of_node/compatible`
	if [ "$compatible" = "gpio-leds" ]; then
		driver="leds-gpio"
	elif [ "$compatible" = "pwm-leds" ]; then
		driver="leds-pwm"
	else
		manufacturer=`echo $compatible | awk -F, '{print $1}'`
		product=`echo $compatible | awk -F, '{print $2}'`
	fi
else
	echo "Unknown device type."
	exit 1
fi

printf "\n#####################################\n"
printf "# LED class device hardware details #\n"
printf "#####################################\n\n"

printf "bus:\t\t\t$bus\n"

if [ ! -z "$idVendor" ]; then
	printf "idVendor:\t\t$idVendor\n"
fi

if [ ! -z "$idProduct" ]; then
	printf "idProduct:\t\t$idProduct\n"
fi

if [ ! -z "$manufacturer" ]; then
	printf "manufacturer:\t\t$manufacturer\n"
fi

if [ ! -z "$product" ]; then
	printf "product:\t\t$product\n"
fi

if [ ! -z "$driver" ]; then
	printf "driver:\t\t\t$driver\n"
fi

if [ ! -z "$input_node" ]; then
	printf "associated input node:\t$input_node\n"
fi

printf "\n####################################\n"
printf "# LED class device name validation #\n"
printf "####################################\n\n"

led_name=`echo $led_cdev_path | sed s'/.*\///'`

num_sections=`echo $led_name | awk -F: '{print NF}'`

if [ $num_sections -eq 1 ]; then
	printf "\":\" delimiter not detected.\t[ FAILED ]\n"
	exit 1
elif [ $num_sections -eq 2 ]; then
	color=`echo $led_name | cut -d: -f1`
	function=`echo $led_name | cut -d: -f2`
elif [ $num_sections -eq 3 ]; then
	devicename=`echo $led_name | cut -d: -f1`
	color=`echo $led_name | cut -d: -f2`
	function=`echo $led_name | cut -d: -f3`
else
	printf "Detected %d sections in the LED class device name - should the script be updated?\n" $num_sections
	exit 1
fi

S_DEV="devicename"
S_CLR="color     "
S_FUN="function  "
status_tab=20

print_msg_ok()
{
	local section_name="$1"
	local section_val="$2"
	local msg="$3"
	printf "$section_name :\t%-${status_tab}.${status_tab}s %s %s\n" "$section_val" "[ OK ]    " "$msg"
}

print_msg_failed()
{
	local section_name="$1"
	local section_val="$2"
	local msg="$3"
	printf "$section_name :\t%-${status_tab}.${status_tab}s %s %s\n" "$section_val" "[ FAILED ]" "$msg"
}

if [ ! -z "$input_node" ]; then
	expected_devname=$input_node
elif [ ! -z "$wifi_phy" ]; then
	expected_devname=$wifi_phy
fi

if [ ! -z "$devicename" ]; then
	if [ ! -z "$expected_devname" ]; then
		if [ "$devicename" = "$expected_devname" ]; then
			print_msg_ok "$S_DEV" "$devicename"
		else
			print_msg_failed "$S_DEV" "$devicename" "Expected: $expected_devname"
		fi
	else
		if [ "$devicename" = "$manufacturer" ]; then
			print_msg_failed "$S_DEV" "$devicename" "Redundant: use of vendor name is discouraged"
		elif [ "$devicename" = "$product" ]; then
			print_msg_failed "$S_DEV" "$devicename" "Redundant: use of product name is discouraged"
		else
			print_msg_failed "$S_DEV" "$devicename" "Unknown devicename - should the script be updated?"
		fi
	fi
elif [ ! -z "$expected_devname" ]; then
	print_msg_failed "$S_DEV" "blank" "Expected: $expected_devname"
fi

if [ ! -z "$color" ]; then
	color_upper=`echo $color | tr [:lower:] [:upper:]`
	color_id_definition=$(cat $led_defs_path | grep "_$color_upper\s" | awk '{print $2}')
	if [ ! -z "$color_id_definition" ]; then
		print_msg_ok "$S_CLR" "$color" "Matching definition: $color_id_definition"
	else
		print_msg_failed "$S_CLR" "$color" "Definition not found in $led_defs_path"
	fi

fi

if [ ! -z "$function" ]; then
	# strip optional enumerator
	function=`echo $function | sed s'/\(.*\)-[0-9]*$/\1/'`
	fun_definition=$(cat $led_defs_path | grep "\"$function\"" | awk '{print $2}')
	if [ ! -z "$fun_definition" ]; then
		print_msg_ok "$S_FUN" "$function" "Matching definition: $fun_definition"
	else
		print_msg_failed "$S_FUN" "$function" "Definition not found in $led_defs_path"
	fi

fi
back to top