You are not logged in.
So, I've been trying to learn how to write modules for the kernel and I wrote a very simple dummy driver. All it does is copy any text that's been written to it in a buffer, and then when read from, it spits that text back out and clears the buffer.
here is the code for the kernel module:
https://github.com/reidiens/mod/blob/main/mod.c
I originally wrote the module in an arch virtual machine and it works perfectly fine. I can write to it multiple times with echo and read all of its contents and clear the buffer by using cat. However, when I wrote the exact same module (same variable names and everything) on my ArcoLinux laptop, using echo to write to the buffer results in this dmesg output:
[ 5070.066750] device created successfully
[ 5098.196388] opened.
[ 5098.197134] BUG: unable to handle page fault for address: 0000645390e24c80
[ 5098.197775] #PF: supervisor read access in kernel mode
[ 5098.198395] #PF: error_code(0x0001) - permissions violation
[ 5098.198998] PGD 171f37067 P4D 171f37067 PUD 171f29067 PMD 186a3a067 PTE 80000001d45f0867
[ 5098.199614] Oops: 0001 [#4] PREEMPT SMP NOPTI
[ 5098.200223] CPU: 1 PID: 10965 Comm: zsh Tainted: P D W OE 6.9.2-arch1-1 #1 0c9f583c4dce67c6fc47fa44804d20ac179c7f14
[ 5098.200852] Hardware name: HP HP Laptop 14-fq0xxx/87B7, BIOS F.51 08/12/2021
[ 5098.201485] RIP: 0010:ruben_write+0x1e/0x40 [ruben]
[ 5098.202132] Code: 90 90 90 90 90 90 90 90 90 90 90 90 f3 0f 1e fa 0f 1f 44 00 00 53 48 85 d2 0f 84 e9 00 00 00 48 8b 05 16 20 00 00 31 db 31 c9 <0f> b6 0c 0e 48 83 c3 01 48 83 c0 01 88 48 ff 48 63 cb 48 39 d1 72
[ 5098.203417] RSP: 0018:ffff9a90c3573db0 EFLAGS: 00010246
[ 5098.204055] RAX: ffffffffc2a187a0 RBX: 0000000000000000 RCX: 0000000000000000
[ 5098.204752] RDX: 0000000000000002 RSI: 0000645390e24c80 RDI: ffff89cbe33dec00
[ 5098.205504] RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000000
[ 5098.206159] R10: 0000000000000000 R11: 0000000000000000 R12: ffff89cbe33dec00
[ 5098.206790] R13: 0000645390e24c80 R14: ffff9a90c3573e58 R15: ffff89cb983a1180
[ 5098.207415] FS: 000072155a7bc080(0000) GS:ffff89ce9f080000(0000) knlGS:0000000000000000
[ 5098.208036] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 5098.208658] CR2: 0000645390e24c80 CR3: 000000010261c000 CR4: 00000000003506f0
[ 5098.209304] Call Trace:
[ 5098.210030] <TASK>
[ 5098.211160] ? __die_body.cold+0x19/0x27
[ 5098.212337] ? page_fault_oops+0x15a/0x2b0
[ 5098.213159] ? exc_page_fault+0x81/0x190
[ 5098.213766] ? asm_exc_page_fault+0x26/0x30
[ 5098.214424] ? ruben_write+0x1e/0x40 [ruben ed82862b0a3bdfefc87b300b506db5d35a8a3c43]
[ 5098.215070] vfs_write+0xf8/0x460
[ 5098.215670] ? srso_return_thunk+0x5/0x5f
[ 5098.216260] ksys_write+0x6d/0xf0
[ 5098.216848] do_syscall_64+0x83/0x190
[ 5098.217431] ? handle_mm_fault+0x1f0/0x300
[ 5098.218010] ? srso_return_thunk+0x5/0x5f
[ 5098.218586] ? do_user_addr_fault+0x34e/0x620
[ 5098.219153] ? srso_return_thunk+0x5/0x5f
[ 5098.219713] ? srso_return_thunk+0x5/0x5f
[ 5098.220270] entry_SYSCALL_64_after_hwframe+0x76/0x7e
[ 5098.220832] RIP: 0033:0x72155a8c6504
[ 5098.221436] Code: c7 00 16 00 00 00 b8 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 f3 0f 1e fa 80 3d 45 0b 0e 00 00 74 13 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 54 c3 0f 1f 00 55 48 89 e5 48 83 ec 20 48 89
[ 5098.222748] RSP: 002b:00007ffe621ab368 EFLAGS: 00000202 ORIG_RAX: 0000000000000001
[ 5098.223377] RAX: ffffffffffffffda RBX: 0000000000000002 RCX: 000072155a8c6504
[ 5098.223993] RDX: 0000000000000002 RSI: 0000645390e24c80 RDI: 0000000000000001
[ 5098.224606] RBP: 00007ffe621ab390 R08: 0000000000000000 R09: 0000000000000000
[ 5098.225224] R10: 000072155ab3cb88 R11: 0000000000000202 R12: 0000000000000002
[ 5098.225846] R13: 0000645390e24c80 R14: 000072155a9a05c0 R15: 000072155a99df00
[ 5098.226560] </TASK>
[ 5098.227567] Modules linked in: mod(OE) ruben(OE) ccm cmac algif_hash algif_skcipher af_alg uvcvideo videobuf2_vmalloc bnep uvc videobuf2_memops videobuf2_v4l2 videodev videobuf2_common mc btusb btrtl btintel btbcm btmtk bluetooth ecdh_generic snd_sof_amd_acp63 snd_sof_amd_vangogh snd_sof_amd_rembrandt snd_sof_amd_renoir snd_sof_amd_acp snd_sof_pci snd_sof_xtensa_dsp snd_sof snd_sof_utils snd_pci_ps intel_rapl_msr snd_amd_sdw_acpi amd_atl soundwire_amd soundwire_generic_allocation soundwire_bus intel_rapl_common snd_ctl_led wl(POE) ledtrig_audio snd_soc_core snd_hda_codec_realtek snd_compress snd_hda_codec_generic ac97_bus snd_hda_scodec_component snd_hda_codec_hdmi kvm_amd snd_pcm_dmaengine rtw88_8822ce snd_rpl_pci_acp6x snd_hda_intel rtw88_8822c snd_acp_pci snd_intel_dspcfg kvm rtw88_pci snd_intel_sdw_acpi snd_acp_legacy_common joydev snd_hda_codec rtw88_core crct10dif_pclmul mousedev snd_hda_core snd_pci_acp6x crc32_pclmul polyval_clmulni snd_hwdep mac80211 polyval_generic gf128mul ghash_clmulni_intel snd_pcm
[ 5098.227711] sha512_ssse3 hid_multitouch sha256_ssse3 snd_pci_acp5x libarc4 sha1_ssse3 snd_rn_pci_acp3x hid_generic aesni_intel snd_timer cfg80211 crypto_simd snd hp_wmi sp5100_tco snd_acp_config cryptd platform_profile snd_soc_acpi vfat rapl sparse_keymap wmi_bmof acpi_cpufreq k10temp snd_pci_acp3x ccp rfkill soundcore i2c_piix4 fat i2c_hid_acpi i2c_hid wireless_hotkey acpi_tad mac_hid vboxnetflt(OE) vboxnetadp(OE) vboxdrv(OE) i2c_dev crypto_user loop dm_mod nfnetlink ip_tables x_tables ext4 crc32c_generic crc16 mbcache jbd2 amdgpu amdxcp i2c_algo_bit drm_ttm_helper ttm drm_exec gpu_sched drm_suballoc_helper crc32c_intel drm_buddy nvme drm_display_helper xhci_pci nvme_core cec xhci_pci_renesas nvme_auth serio_raw atkbd libps2 vivaldi_fmap i8042 video serio wmi
[ 5098.233319] Unloaded tainted modules: ruben(OE):4 [last unloaded: ruben(OE)]
[ 5098.237792] CR2: 0000645390e24c80
[ 5098.238656] ---[ end trace 0000000000000000 ]---
[ 5098.239422] RIP: 0010:ruben_write+0x1e/0x40 [ruben]
[ 5098.240159] Code: 90 90 90 90 90 90 90 90 90 90 90 90 f3 0f 1e fa 0f 1f 44 00 00 53 48 85 d2 0f 84 e9 00 00 00 48 8b 05 16 20 00 00 31 db 31 c9 <0f> b6 0c 0e 48 83 c3 01 48 83 c0 01 88 48 ff 48 63 cb 48 39 d1 72
[ 5098.241623] RSP: 0018:ffff9a90c33f7c20 EFLAGS: 00010246
[ 5098.242361] RAX: ffffffffc2a187a0 RBX: 0000000000000000 RCX: 0000000000000000
[ 5098.243228] RDX: 0000000000000004 RSI: 0000636d28c71c80 RDI: ffff89cbbb38b700
[ 5098.244440] RBP: 0000000000000000 R08: 0000000000000000 R09: 0000000000000000
[ 5098.245761] R10: 0000000000000000 R11: 0000000000000000 R12: ffff89cbbb38b700
[ 5098.246718] R13: 0000636d28c71c80 R14: ffff9a90c33f7cc8 R15: ffff89cbd3552300
[ 5098.247530] FS: 000072155a7bc080(0000) GS:ffff89ce9f080000(0000) knlGS:0000000000000000
[ 5098.248359] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 5098.249103] CR2: 0000645390e24c80 CR3: 000000010261c000 CR4: 00000000003506f0
[ 5098.249840] note: zsh[10965] exited with irqs disabled
[ 5098.251618] closed.
[ 5262.770223] destroying device "ruben"...
[ 5262.773156] Destroyed.
[ 5262.774350] Destroying device class "r_dev_class"...
[ 5262.775144] Destroyed.
[ 5262.775797] Removing "r_cdev"...
[ 5262.776533] Removed.
[ 5262.777153] Unregistering chrdev region "ruben_dev"...The dmesg output says it has something to do with having the wrong permissions, but the buffer is not writing over any RO memory and I've been running it as root. I appreciate any and all help/constructive criticism, as I am still learning.
Last edited by tibamita (2024-05-30 15:33:41)
there is beauty and magic in the air
Offline
I would suggest looking at https://linux-kernel-labs.github.io/ref … -and-write with respect to copying data to and from userspace.
Offline
I would suggest looking at https://linux-kernel-labs.github.io/ref … -and-write with respect to copying data to and from userspace.
This was insightful, thank you! I appreciate it
there is beauty and magic in the air
Offline