From ce216152dd709861942726523aae47025d02d07a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Mon, 8 Oct 2018 00:00:00 +0000 Subject: [PATCH] kernel cli: rw by default --- README.adoc | 50 ++++++++++++++++++++++++-------------- rootfs-post-build-script | 4 +++ rootfs_overlay/etc/inittab | 4 +-- run | 2 +- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/README.adoc b/README.adoc index 1a6d9d0..09560a6 100644 --- a/README.adoc +++ b/README.adoc @@ -745,7 +745,7 @@ The main reason this setup is included in this project, despite the word "Linux" This setup allows you to make a tiny OS and that runs just a few instructions, use it to fully control the CPU to better understand the simulators for example, or develop your own OS if you are into that. -You can also use C and a subset of the C standard library because we enable link:https://en.wikipedia.org/wiki/Newlib[Newlib] by default. +You can also use C and a subset of the C standard library because we enable link:https://en.wikipedia.org/wiki/Newlib[Newlib] by default. See also: https://electronics.stackexchange.com/questions/223929/c-standard-libraries-on-bare-metal/400077#400077 Our C bare-metal compiler is built with link:https://github.com/crosstool-ng/crosstool-ng[crosstool-NG]. If you have already built <> previously, you will end up with two GCCs installed. Unfortunately I don't see a solution for this, since we need separate toolchains for Newlib on baremetal and glibc on Linux: https://stackoverflow.com/questions/38956680/difference-between-arm-none-eabi-and-arm-linux-gnueabi/38989869#38989869 @@ -2452,7 +2452,7 @@ We disable networking by default because it starts an userland process, and we w Enable: .... -/sbin/ifup -a +ifup -a .... That command goes over all (`-a) the interfaces in `/etc/network/interfaces` and brings them up. @@ -2460,7 +2460,7 @@ That command goes over all (`-a) the interfaces in `/etc/network/interfaces` and Disable: .... -/sbin/ifdown -a +ifdown -a .... Test: @@ -3590,9 +3590,9 @@ core_param(panic, panic_timeout, int, 0644); ==== rw -By default, the root filesystem is mounted as readonly. TODO rationale? +By default, the Linux kernel mounts the root filesystem as readonly. TODO rationale? -This cannot be obesrved, because by default our link:rootfs_overlay/etc/inittab[] does: +This cannot be observed in the default BusyBox init, because by default our link:rootfs_overlay/etc/inittab[] does: .... /bin/mount -o remount,rw / @@ -3606,13 +3606,15 @@ UUID=/dev/sda1 / ext4 errors=remount-ro 0 1 which uses default mount `rw` flags. -To observe the default readonly behaviour, <> with a raw shell: +We have however removed those setups init setups to keep things more minimal, and replaced them with the `rw` kernel boot parameter makes the root mounted as writable. + +To observe the default readonly behaviour, hack the link:run[] script to remove <>, and then run on a raw shell: .... ./run --kernel-cli 'init=/bin/sh' .... -and then try to: +Now try to do: .... touch a @@ -3639,12 +3641,6 @@ which contains: and so it is Read Only as shown by `ro`. -So finally we can observe that the `rw` kernel boot paramter makes the root mounted as writable: - -.... -./run --kernel-cli 'init=/bin/sh rw' -.... - ==== norandmaps Disable userland address space randomization. Test it out by running <> twice: @@ -6387,9 +6383,11 @@ although we cannot change between terminals from there. Each populated TTY contains a "shell": -* `-/bin/sh`: goes directly into an `sh` without a login prompt. Don't forget the dash `-`: https://askubuntu.com/questions/902998/how-to-check-which-tty-am-i-using +* `-/bin/sh`: goes directly into an `sh` without a login prompt. + -TODO: does not work for the `ttyS*` terminals. Why? +The trailing dash `-` can be used on any command. It makes the command that follows take over the TTY, which is what we typically want for interactive shells: https://askubuntu.com/questions/902998/how-to-check-which-tty-am-i-using ++ +The `getty` executable however also does this operation and therefore dispenses the `-`. * `/sbin/getty` asks for password, and then gives you an `sh` + We can overcome the password prompt with the `-l /loginroot.sh` technique explained at: https://askubuntu.com/questions/902998/how-to-check-which-tty-am-i-using but I don't see any advantage over `-/bin/sh` currently. @@ -7992,7 +7990,7 @@ EXT4-fs (sda): re-mounted. Opts: block_validity,barrier,user_xattr TODO replay with network gets stuck: .... -./qemu-rr --eval-busybox '/sbin/ifup -a;wget -S google.com;/poweroff.out;' +./qemu-rr --eval-busybox 'ifup -a;wget -S google.com;/poweroff.out;' .... after the message: @@ -9480,7 +9478,7 @@ clock=500 Each node has: -* a list of child nodes, e.g. `system` is a child of `root`, and both `cpu` and `cpu_clk_domain` are children of +* a list of child nodes, e.g. `system` is a child of `root`, and both `cpu` and `cpu_clk_domain` are children of `system` * a list of parameters, e.g. `system.semihosting` is `Null`, which means that <> was turned off ** the `type` parameter shows is present on every node, and it maps to a `Python` object that inherits from `SimObject`. + @@ -9704,6 +9702,22 @@ which gives: uid=1000(user0) gid=1000(user0) groups=1000(user0) .... +==== Login as a non-root user without password + +Replace on `inittab`: + +.... +::respawn:-/bin/sh +.... + +with: + +.... +::respawn:-/bin/login -f user0 +.... + +`-f` forces login without asking for the password. + === Add new Buildroot packages First, see if you can't get away without actually adding a new package, for example: @@ -11021,7 +11035,7 @@ Source: link:test-kernel-module[] Test that the Internet works: .... -./run --arch x86_64 --kernel-cli '- lkmc_eval="/sbin/ifup -a;wget -S google.com;poweroff;"' +./run --arch x86_64 --kernel-cli '- lkmc_eval="ifup -a;wget -S google.com;poweroff;"' .... Source: link:rootfs_overlay/test_all.sh[]. diff --git a/rootfs-post-build-script b/rootfs-post-build-script index f575be8..1a9a480 100755 --- a/rootfs-post-build-script +++ b/rootfs-post-build-script @@ -3,10 +3,14 @@ # make target-finalize # which gets called by the default target. target_dir="$1" +# /dev/* entries were taken out of BusyBox inittab, +# no need to do that on every boot, right? mkdir -p \ "${target_dir}/mnt/9p/data" \ "${target_dir}/mnt/9p/out" \ "${target_dir}/mnt/9p/rootfs_overlay" \ + "${target_dir}/dev/pts" \ + "${target_dir}/dev/shm" \ ; # Maybe there is a cleaner way to get rid of those files, # like disabling some Buildroot packages, but no patience. diff --git a/rootfs_overlay/etc/inittab b/rootfs_overlay/etc/inittab index 5d0318e..afdfb41 100644 --- a/rootfs_overlay/etc/inittab +++ b/rootfs_overlay/etc/inittab @@ -1,13 +1,11 @@ ::sysinit:/bin/mount -t proc proc /proc -::sysinit:/bin/mount -o remount,rw / ::sysinit:/bin/mkdir -p /dev/pts ::sysinit:/bin/mkdir -p /dev/shm ::sysinit:/bin/mount -a ::sysinit:/bin/hostname -F /etc/hostname ::sysinit:/etc/init.d/rcS ::respawn:-/bin/sh -# Same as above: console is the default. -#console::respawn:-/bin/sh +ttyS1::respawn:-/bin/sh ::ctrlaltdel:/sbin/reboot ::shutdown:/etc/init.d/rcK ::shutdown:/sbin/swapoff -a diff --git a/run b/run index 1c5ee74..37c129a 100755 --- a/run +++ b/run @@ -46,7 +46,7 @@ def main(args, extra_args=None): # * https://unix.stackexchange.com/questions/397939/turning-off-kaslr-to-debug-linux-kernel-using-qemu-and-gdb # * https://stackoverflow.com/questions/44612822/unable-to-debug-kernel-with-qemu-gdb/49840927#49840927 # Turned on by default since v4.12 - kernel_cli = 'console_msg_format=syslog nokaslr norandmaps panic=-1 printk.devkmsg=on printk.time=y' + kernel_cli = 'console_msg_format=syslog nokaslr norandmaps panic=-1 printk.devkmsg=on printk.time=y rw' if args.kernel_cli is not None: kernel_cli += ' {}'.format(args.kernel_cli) kernel_cli_after_dash = ''