c
static ssize_t bnad_debugfs_write_regrd(struct file *file, const char __user *buf,
                      size_t nbytes, loff_t *ppos) {
    void *kern_buf;

    // ... other code ...

    // Allocate kernel buffer with size validation
    kern_buf = kmalloc(nbytes, GFP_KERNEL);
    if (!kern_buf) {
        return -ENOMEM; 
    }

    // Use copy_from_user for safe copying
    if (copy_from_user(kern_buf, buf, nbytes) != 0) {
        kfree(kern_buf);
        return -EFAULT;
    }

    // ... rest of the function ... 
}
