Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 560411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:20:26+00:00 2026-05-13T12:20:26+00:00

I’m trying to allocate some memory for a char* as follows. static ssize_t memo_write(struct

  • 0

I’m trying to allocate some memory for a char* as follows.

static ssize_t memo_write(struct file *filp, const char __user *buf, 
   size_t count, loff_t *f_pos){
    ssize_t retval = -ENOMEM;
    printk("write function\n");

    if((data = kmalloc(strlen(buf), GFP_KERNEL)) == NULL)
        printk("kmalloc fail\n");

    if(copy_from_user(data, buf, strlen(buf))){
        retval = -EFAULT;
        goto out;
    }
    *f_pos += strlen(buf);
    retval = strlen(buf);

    out:
        return retval;
}

‘data’ is declared in a header file as

char *data;

When I call the write function, the ‘kmalloc fail’ line isn’t reached, which leads me to believe the kmalloc succeeded, however the data isn’t displayed when I try to read from the ‘data’ variable again.

More confusingly, if I get rid of the kmalloc bit altogether, the data can be read from the driver. Although the problem then is it is followed by a load of other data because i don’t have the opportunity to memset() it.

Am I using kmalloc correctly? Presumably not. How should I be doing this?

Additionally, my read function is as follows.

static ssize_t memo_read(struct file *f, char __user *buf, 
    size_t count, loff_t *f_pos){
    ssize_t retval = 0;

    printk("read function\n");
    printk("data = %s\n", data);

    if(*f_pos >= strlen(data)){
        printk("EOF\n");
        goto out;
    }

    if(copy_to_user(buf, data, strlen(data))){
        retval = -EFAULT;
        goto out;
    }
    printk("copy_to_user success\n");
    *f_pos += strlen(data);
    retval = strlen(data);
    out:
        return retval;
}

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:20:26+00:00Added an answer on May 13, 2026 at 12:20 pm

    You should be using strlen_user() on the userspace pointer, instead of strlen() – and you should only call it once, and keep the result around (otherwise, you have a potential kernel exploit, because a second userspace thread could change the buffer while you’re working on it).

    Alternatively, you could use strncpy_from_user().

    Apart from that, the kmalloc looks OK.


    (But really, as ephemient says, you should rethink your whole approach and use the count argument instead of treating the input as a string).


    Since you can’t rely on the data written to a file being nul-terminated strings, you’ll need to keep a data_len length parameter around alongside the data. Then your read/write implementations would be along these lines:

    static char *data = NULL;
    static size_t data_len;
    static DEFINE_MUTEX(data_mutex);
    
    static ssize_t memo_read(struct file *f, char __user *buf, size_t count, loff_t *f_pos
    {
        ssize_t retval = 0;
        char *start;
    
        mutex_lock(&data_mutex);
    
        if (!data)
        {
            retval = -EINVAL; /* Or whatever you want to do here... */
            goto out;
        }
    
        if (*f_pos >= data_len)
            goto out; /* EOF */
    
        start = data + *f_pos;
        retval = data_len - *f_pos;
    
        if (retval > count)
            retval = count;
    
        if (copy_to_user(buf, start, retval))
        {
            retval = -EFAULT;
            goto out;
        }
    
        *f_pos += retval;
    
    out:
        mutex_unlock(&data_mutex);
        return retval;
    }
    
    static ssize_t memo_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
    {
        ssize_t retval = -ENOMEM;
    
        mutex_lock(&data_mutex);
    
        if (data)
            kfree(data);
    
        data = kmalloc(count, GFP_KERNEL);
    
        if (!data)
            goto out;
    
        if (copy_from_user(data, buf, count))
        {
            kfree(data);
            retval = -EFAULT;
            goto out;
        }
    
        *f_pos = count;
        retval = count;
        data_len = count;
    
    out:
        mutex_unlock(&data_mutex);
        return retval;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.