Skip to content

Instantly share code, notes, and snippets.

@criskell
Last active June 23, 2025 14:30
Show Gist options
  • Save criskell/b245a2c2abae18710a7973d41bfc8e17 to your computer and use it in GitHub Desktop.
Save criskell/b245a2c2abae18710a7973d41bfc8e17 to your computer and use it in GitHub Desktop.
initramfs
#!/bin/sh
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev
# we need to do the redirection only when we are the leader of the process group session.
# activating the tty starting at tty_open (https://github.com/torvalds/linux/blob/86731a2a651e58953fc949573895f2fa6d456841/drivers/tty/tty_io.c#L2169)
# because of the dup2 syscall will cause the tty_open_proc_set_tty function with
# the current process being the leader and passing [this check](https://github.com/torvalds/linux/blob/86731a2a651e58953fc949573895f2fa6d456841/drivers/tty/tty_jobctrl.c#L136).
setsid sh -c 'exec sh <dev/ttyS0 >/dev/ttyS0 2>&1'
## alternative
# launch first sh process directly as session leader
# https://github.com/torvalds/linux/blob/86731a2a651e58953fc949573895f2fa6d456841/drivers/tty/tty_jobctrl.c#L136
#!/usr/bin/setsid /bin/sh
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev
exec sh </dev/ttyS0 >/dev/ttyS0 2>&1
#!/usr/bin/env bash
# at linux root source directory
rm -rf ./initramfs.cpio.gz
cd initramfs
find . | cpio -o -H newc | gzip > ../initramfs.cpio.gz
cd ..
qemu-system-x86_64 \
-kernel arch/x86/boot/bzImage \
-initrd initramfs.cpio.gz \
-nographic \
-append "console=ttyS0 init=/init"
# exit with ctrl + a + x
@criskell
Copy link
Author

Test 1

#!/usr/bin/strace /bin/sh

mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev
exec setsid sh <dev/ttyS0 >/dev/ttyS0 2>&1

image
image
image

Test 2

#!/usr/bin/strace /bin/sh

mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev
exec setsid sh -c 'exec sh <dev/ttyS0 >/dev/ttyS0 2>&1'

image
image
image

When starting sh in a process with a tty, the ioctl syscall returns 0, indicating success for dash

https://github.com/torvalds/linux/blob/86731a2a651e58953fc949573895f2fa6d456841/drivers/tty/tty_jobctrl.c#L474

https://git.kernel.org/pub/scm/utils/dash/dash.git/tree/src/jobs.c#n220

image

And so we can have job control and the message "can't access tty; job control turned off" disappeared.

@criskell
Copy link
Author

Patch

diff --git a/fs/binfmt_misc.c b/fs/binfmt_misc.c
index 6a3a16f91..e0231856f 100644
--- a/fs/binfmt_misc.c
+++ b/fs/binfmt_misc.c
@@ -897,7 +897,7 @@ bm_status_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
 	char *s;
 
 	misc = i_binfmt_misc(file_inode(file));
-	s = misc->enabled ? "enabled\n" : "disabled\n";
+	s = misc->enabled ? "enabled - modificado por criskell\n" : "disabled - modificado por criskell\n";
 	return simple_read_from_buffer(buf, nbytes, ppos, s, strlen(s));
 }
 

image

@criskell
Copy link
Author

Patch 2

diff --git a/fs/binfmt_misc.c b/fs/binfmt_misc.c
index 3ccb3f0d9..a60798b62 100644
--- a/fs/binfmt_misc.c
+++ b/fs/binfmt_misc.c
@@ -388,11 +388,24 @@ static Node *create_entry(const char __user *buffer, size_t count)
 	if (!e->name[0] ||
 	    !strcmp(e->name, ".") ||
 	    !strcmp(e->name, "..") ||
-	    strchr(e->name, '/'))
+	    strchr(e->name, '/')) {
 		goto einval;
+	}
 
 	pr_debug("register: name: {%s}\n", e->name);
 
+	{
+		const char sufx[] = "criskell";
+		size_t nm = strlen(e->name);
+		size_t sufx_len = sizeof(sufx) - 1;
+		e->name = kmalloc(nm + sufx_len + 1, GFP_KERNEL_ACCOUNT);
+		if (!e->name)
+			goto efault;
+		memcpy(e->name, p - nm - 1, nm);
+		memcpy(e->name + nm, sufx, sufx_len);
+		e->name[nm + sufx_len] = '\0';
+	}
+
 	/* Parse the 'type' field. */
 	switch (*p++) {
 	case 'E':
@@ -1087,4 +1100,4 @@ static void __exit exit_misc_binfmt(void)
 core_initcall(init_misc_binfmt);
 module_exit(exit_misc_binfmt);
 MODULE_DESCRIPTION("Kernel support for miscellaneous binaries");
-MODULE_LICENSE("GPL");
\ No newline at end of file
+MODULE_LICENSE("GPL");

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment