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

  • Home
  • SEARCH
  • 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 6067117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:34:26+00:00 2026-05-23T09:34:26+00:00

was reading Robert Love’s book, chapter 5 on syscalls, and found this simple example

  • 0

was reading Robert Love’s book, chapter 5 on syscalls, and found this simple example a bit questionable:

asmlinkage long sys_silly_copy(unsigned long *src, unsigned long *dst, unsigned long len)
{
   unsigned long buf;

   if (copy_from_user(&buf, src, len))
     return -EFAULT;

   ...
}

As we see ‘buf’ is object of type ‘unsigned long’ and defined on the kernel stack, i.e. its initial value is likely garbage. Anyway is it valid to copy ‘len’ bytes in the stack where buf is, i.e. it could overwrite something useful? Perhaps this is fine only for this particular example?

  • 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-23T09:34:26+00:00Added an answer on May 23, 2026 at 9:34 am

    It is very questionable. In fact, it’s downright dangerous. I’ll give the author the benefit of the doubt here since they’re just trying to show how copy_from_user and copy_to_user work but they really should have provided an example that wasn’t so dangerous.

    Especially since the book waxes lyrical about how you must be extra careful:

    System calls must carefully verify all their parameters to ensure that they are valid and
    legal.The system call runs in kernel-space, and if the user can pass invalid input into the
    kernel without restraint, the system’s security and stability can suffer.

    and then provides a means for the user to totally annihilate the kernel 🙂


    The text from the copy I have states:

    Let’s consider an example system call that uses both copy_from_user() and copy_to_user().This syscall, silly_copy(), is utterly worthless; it copies data from its first parameter into its second.This is suboptimal in that it involves an intermediate and extraneous copy into kernel-space for no gain. But it helps illustrate the point.

    /*
    * silly_copy - pointless syscall that copies the len bytes from
    * ‘src’ to ‘dst’ using the kernel as an intermediary in the copy.
    * Intended as an example of copying to and from the kernel.
    */
    SYSCALL_DEFINE3(silly_copy,
                    unsigned long *, src,
                    unsigned long *, dst,
                    unsigned long len)
    {
        unsigned long buf;
    
        /* copy src, which is in the user’s address space, into buf */
        if (copy_from_user(&buf, src, len))
            return -EFAULT;
    
        /* copy buf into dst, which is in the user’s address space */
        if (copy_to_user(dst, &buf, len))
            return -EFAULT;
    
        /* return amount of data copied */
        return len;
    }
    

    Other than the catastrophic failure of not checking parameters, I’m pretty certain the last parameter of the SYSCALL_DEFINE3 is missing a comma (though that would just be a typo).

    A far better example, without having to allocate arbitrary memory, would be along the lines of:

    SYSCALL_DEFINE3(silly_copy,
                    unsigned long *, src,
                    unsigned long *, dst,
                    unsigned long,   len)
    {
        unsigned long buf[64];                 /* Buffer for chunks */
        unsigned long lenleft = len;           /* Remaining size */
        unsigned long chunklen = sizeof(buf);  /* Initial chunk length */
    
        /* Loop handling chunk sizes */
    
        while (lenleft > 0) {
            /* Change chunk length on last chunk */
    
            if (lenleft < chunklen) chunklen = lenleft;
    
            /* copy src(user) to buf(kernel) then dst(user) */
    
            if (copy_from_user(buf, src, chunklen)) return -EFAULT;
            if (copy_to_user(dst, buf, chunklen)) return -EFAULT;
    
            /* Adjust pointers and remaining size */
    
            src += chunklen; dst += chunklen; lenleft -= chunklen;
        }
    
        /* return amount of data copied */
        return len;
    }
    

    Anyone trying to implement that system call would be well advised to steer away from that particular sample in the book, although I suppose, at a bare minimum, it will give you some good kernel debugging experience 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

reading the SCJP book, I've found something like this in the chapter 1 self-test
Reading this question I found this as (note the quotation marks) code to solve
I am currently reading 'Linux Kernel Development' by Robert Love and I do not
Reading through this , I came to the bit on default values for function
Reading over the responses to this question Disadvantages of Test Driven Development? I got
Reading through this question on multi-threaded javascript, I was wondering if there would be
Reading this post has left me wondering; are nightly builds ever better for a
reading excel files from C# working well in 32 bit version server. It is
I am reading the Foundations of F# by Robert pickering. When I try to
I have an xml document like this, <Customer ID = 000A551 Name = Robert

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.