0

I am writing a string from my kernel module to /proc and trying to read in the user space but getting an error. What am I doing wrong in the below code? Writing just an integer or a hardcoded string works fine, but writing a string variable gives an error. Eventually, I want to write a static global string that will be updated by other functions in the module.

UPDATE: I noticed that when I update the file name in proc_create, it works for the first time and after that gives segmentation fault on reusing the same file. I have added more code for reference.

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)
#define HAVE_PROC_OPS
#endif
#define BUFSIZE 100

static struct proc_dir_entry *ent;

static ssize_t myread(struct file *file, char __user *ubuf,size_t count, loff_t *ppos) 
{
    char buf[BUFSIZE];
    char logger_buffer[] = "yoloTest";
    int len=0;
    if(*ppos > 0 || count < BUFSIZE)
        return 0;

    len += sprintf(buf,"var1 = %d\n",var1value);
    // len += sprintf(buf + len,"mode = testmode\n") This works fine and I can see the output using cat
    len += sprintf(buf + len,"mode = %s\n",logger_buffer); // This says "killed" on cat and gives below log
    
    if(copy_to_user(ubuf,buf,len))
        return -EFAULT;

    *ppos = len;
    return len;
}
#ifdef HAVE_PROC_OPS 
    static const struct proc_ops myops = 
    {
        .owner = THIS_MODULE,
        .proc_read = myread,
        // .proc_write = mywrite,
    };
#else
    static const struct file_operations myops = { 
        .read = myread, 
        // .write = mywrite,
    }; 
#endif 

In init func: ent=proc_create("interceptor4",0660,NULL,&myops);

Error Log

Mar 15 21:17:27 x-pc kernel: [900431.046494] BUG: unable to handle page fault for address: ffffffffbe197400
Mar 15 21:17:27 x-pc kernel: [900431.046496] #PF: supervisor instruction fetch in kernel mode
Mar 15 21:17:27 x-pc kernel: [900431.046496] #PF: error_code(0x0010) - not-present page
Mar 15 21:17:27 x-pc kernel: [900431.046497] PGD 42f40e067 P4D 42f40e067 PUD 42f40f063 PMD 0 
Mar 15 21:17:27 x-pc kernel: [900431.046499] Oops: 0010 [#2] SMP PTI
Mar 15 21:17:27 x-pc kernel: [900431.046500] CPU: 3 PID: 20660 Comm: cat Tainted: P      D W  OE     5.4.0-77-generic #86~18.04.1-Ubuntu
Mar 15 21:17:27 x-pc kernel: [900431.046501] Hardware name: System manufacturer System Product Name/STRIX Z270I GAMING, BIOS 1205 05/11/2018
Mar 15 21:17:27 x-pc kernel: [900431.046503] RIP: 0010:0xffffffffbe197400
Mar 15 21:17:27 x-pc kernel: [900431.046504] Code: Bad RIP value.
Mar 15 21:17:27 x-pc kernel: [900431.046505] RSP: 0018:ffffbba188bc7c20 EFLAGS: 00010286
Mar 15 21:17:27 x-pc kernel: [900431.046506] RAX: ffffffffbe197400 RBX: ffff928d3c5260c0 RCX: 0000000000000000
Mar 15 21:17:27 x-pc kernel: [900431.046506] RDX: 0000000000000028 RSI: ffff928a36853600 RDI: ffff928a45430048
Mar 15 21:17:27 x-pc kernel: [900431.046507] RBP: ffffbba188bc7c50 R08: ffffdba17f8d2480 R09: ffff928cd02776b8
Mar 15 21:17:27 x-pc kernel: [900431.046508] R10: ffffbba188bc7c30 R11: 5f534c0032726f74 R12: ffff928a45430048
Mar 15 21:17:27 x-pc kernel: [900431.046508] R13: ffff928a36853600 R14: 00000000fffffff4 R15: ffff928cd02776b8
Mar 15 21:17:27 x-pc kernel: [900431.046509] FS:  00007fc1d110a540(0000) GS:ffff928d3e8c0000(0000) knlGS:0000000000000000
Mar 15 21:17:27 x-pc kernel: [900431.046510] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
Mar 15 21:17:27 x-pc kernel: [900431.046510] CR2: ffffffffbe1973d6 CR3: 000000043a0b8004 CR4: 00000000003606e0
Mar 15 21:17:27 x-pc kernel: [900431.046511] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
Mar 15 21:17:27 x-pc kernel: [900431.046511] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Mar 15 21:17:27 x-pc kernel: [900431.046512] Call Trace:
Mar 15 21:17:27 x-pc kernel: [900431.046515]  ? proc_reg_open+0x74/0x120
Mar 15 21:17:27 x-pc kernel: [900431.046516]  ? proc_put_link+0x10/0x10
Mar 15 21:17:27 x-pc kernel: [900431.046518]  do_dentry_open+0x1df/0x3a0
Mar 15 21:17:27 x-pc kernel: [900431.046519]  vfs_open+0x2f/0x40
Mar 15 21:17:27 x-pc kernel: [900431.046520]  path_openat+0x2f9/0x16a0
Mar 15 21:17:27 x-pc kernel: [900431.046521]  ? unlock_page_memcg+0x12/0x20
Mar 15 21:17:27 x-pc kernel: [900431.046523]  ? page_add_file_rmap+0x13a/0x180
Mar 15 21:17:27 x-pc kernel: [900431.046525]  ? filemap_map_pages+0x181/0x3b0
Mar 15 21:17:27 x-pc kernel: [900431.046526]  do_filp_open+0x9b/0x110
Mar 15 21:17:27 x-pc kernel: [900431.046527]  ? __check_object_size+0xdb/0x1b0
Mar 15 21:17:27 x-pc kernel: [900431.046529]  ? __alloc_fd+0x46/0x170
Mar 15 21:17:27 x-pc kernel: [900431.046530]  do_sys_open+0x1ba/0x2e0
Mar 15 21:17:27 x-pc kernel: [900431.046530]  ? do_sys_open+0x1ba/0x2e0
Mar 15 21:17:27 x-pc kernel: [900431.046532]  __x64_sys_openat+0x20/0x30
Mar 15 21:17:27 x-pc kernel: [900431.046534]  do_syscall_64+0x57/0x190
Mar 15 21:17:27 x-pc kernel: [900431.046536]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
Mar 15 21:17:27 x-pc kernel: [900431.046536] RIP: 0033:0x7fc1d0c2ad5e
Mar 15 21:17:27 x-pc kernel: [900431.046537] Code: 25 00 00 41 00 3d 00 00 41 00 74 48 48 8d 05 91 0c 2e 00 8b 00 85 c0 75 69 89 f2 b8 01 01 00 00 48 89 fe bf 9c ff ff ff 0f 05 <48> 3d 00 f0 ff ff 0f 87 a6 00 00 00 48 8b 4c 24 28 64 48 33 0c 25
Mar 15 21:17:27 x-pc kernel: [900431.046538] RSP: 002b:00007fff7e3ca8e0 EFLAGS: 00000246 ORIG_RAX: 0000000000000101
Mar 15 21:17:27 x-pc kernel: [900431.046539] RAX: ffffffffffffffda RBX: 0000000000000001 RCX: 00007fc1d0c2ad5e
Mar 15 21:17:27 x-pc kernel: [900431.046539] RDX: 0000000000000000 RSI: 00007fff7e3cc87f RDI: 00000000ffffff9c
Mar 15 21:17:27 x-pc kernel: [900431.046540] RBP: 0000556297593b80 R08: 0000000000000000 R09: 0000000000000000
Mar 15 21:17:27 x-pc kernel: [900431.046540] R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
Mar 15 21:17:27 x-pc kernel: [900431.046541] R13: 00007fff7e3cabb0 R14: 00007fff7e3cabb8 R15: 0000000000020000
Mar 15 21:17:27 x-pc kernel: [900431.046542] Modules linked in: usbhid myusbkbd(OE) nfnetlink_queue nfnetlink_log nf_conntrack_netlink nfnetlink xfrm_user xfrm_algo br_netfilter rfcomm xt_CHECKSUM iptable_mangle xt_MASQUERADE iptable_nat bridge stp llc ebtable_filter ebtables aufs snd_hda_codec_hdmi nvidia_uvm(POE) overlay bnep intel_rapl_msr mei_hdcp nvidia_drm(POE) nvidia_modeset(POE) intel_rapl_common x86_pkg_temp_thermal nvidia(POE) intel_powerclamp coretemp snd_hda_codec_realtek snd_hda_codec_generic ledtrig_audio kvm_intel binfmt_misc snd_hda_intel snd_intel_dspcfg kvm snd_hda_codec crct10dif_pclmul snd_hda_core crc32_pclmul nls_iso8859_1 ath10k_pci snd_hwdep ghash_clmulni_intel snd_pcm ath10k_core aesni_intel ath crypto_simd snd_seq_midi mac80211 snd_seq_midi_event cryptd snd_rawmidi cfg80211 glue_helper drm_kms_helper rapl intel_cstate drm snd_seq btusb btrtl ipmi_devintf snd_seq_device btbcm snd_timer btintel eeepc_wmi ipmi_msghandler bluetooth asus_wmi fb_sys_fops sparse_keymap snd syscopyarea ecdh_generic
Mar 15 21:17:27 x-pc kernel: [900431.046561]  sysfillrect wmi_bmof joydev input_leds mxm_wmi libarc4 ecc sysimgblt soundcore mei_me mei mac_hid acpi_pad ip6t_REJECT nf_reject_ipv6 nf_log_ipv6 xt_hl ip6t_rt ipt_REJECT nf_reject_ipv4 nf_log_ipv4 nf_log_common xt_LOG xt_limit xt_tcpudp xt_addrtype xt_conntrack ip6table_filter ip6_tables sch_fq_codel nf_conntrack_netbios_ns nf_conntrack_broadcast nf_nat_ftp nf_nat nf_conntrack_ftp nf_conntrack nf_defrag_ipv6 nf_defrag_ipv4 libcrc32c ppdev iptable_filter bpfilter lp parport ip_tables x_tables autofs4 hid_generic hid nvme e1000e ahci nvme_core libahci video wmi [last unloaded: usbhid]
Mar 15 21:17:27 x-pc kernel: [900431.046574] CR2: ffffffffbe197400
Mar 15 21:17:27 x-pc kernel: [900431.046575] ---[ end trace 722ba9e4030e36ed ]---
Mar 15 21:17:27 x-pc kernel: [900431.046576] RIP: 0010:0xffffffffbe197400
Mar 15 21:17:27 x-pc kernel: [900431.046577] Code: Bad RIP value.
Mar 15 21:17:27 x-pc kernel: [900431.046578] RSP: 0018:ffffbba188aefc20 EFLAGS: 00010286
Mar 15 21:17:27 x-pc kernel: [900431.046578] RAX: ffffffffbe197400 RBX: ffff928d3c5260c0 RCX: 0000000000000000
Mar 15 21:17:27 x-pc kernel: [900431.046579] RDX: 0000000000000028 RSI: ffff928a20a0d500 RDI: ffff928a45430048
Mar 15 21:17:27 x-pc kernel: [900431.046579] RBP: ffffbba188aefc50 R08: ffffdba17f992480 R09: ffff928cd7c3fb90
Mar 15 21:17:27 x-pc kernel: [900431.046580] R10: ffffbba188aefc30 R11: ffff928ceaea0d80 R12: ffff928a45430048
Mar 15 21:17:27 x-pc kernel: [900431.046580] R13: ffff928a20a0d500 R14: 00000000fffffff4 R15: ffff928cd7c3fb90
Mar 15 21:17:27 x-pc kernel: [900431.046581] FS:  00007fc1d110a540(0000) GS:ffff928d3e8c0000(0000) knlGS:0000000000000000
Mar 15 21:17:27 x-pc kernel: [900431.046582] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
Mar 15 21:17:27 x-pc kernel: [900431.046582] CR2: ffffffffbe1973d6 CR3: 000000043a0b8004 CR4: 00000000003606e0
Mar 15 21:17:27 x-pc kernel: [900431.046583] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
Mar 15 21:17:27 x-pc kernel: [900431.046583] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Progman
  • 16,827
  • 6
  • 33
  • 48
tarun14110
  • 940
  • 5
  • 26
  • 57
  • The error message contains a call trace with `open` call. It smells like you have done something wrong **before** the `read` call, and when your code is executed, the system is already in a bad state. If you think your `sprintf` calls are wrong, then you could test them inside the module's init function, without creating a file in procfs. – Tsyvarev Mar 16 '22 at 07:24
  • `sprintf` works fine. I think the issue is something else like you said. I updated the question with more code. It works fine with the string for the first time on updating the file name in `proc_create`. After that, if I remove the module and reload again using the same file name in `proc_create` it gives me a segmentation fault while reading through `cat`. Is it expected? – tarun14110 Mar 16 '22 at 17:57
  • Have you delete the file in the ``exit`` function of your module? – Tsyvarev Mar 16 '22 at 18:24
  • I was not deleting it earlier. After removing the module I couldn't access the /proc/filename. So I assumed it's deleted by default. My bad. Now after adding `remove_proc_entry("interceptor4", NULL);` in exit function, it works fine. Thanks for the help. – tarun14110 Mar 16 '22 at 20:27

0 Answers0