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 7728725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:47:27+00:00 2026-06-01T05:47:27+00:00

i have to make a device driver that returns a random number between 1

  • 0

i have to make a device driver that returns a random number between 1 – 50 and am confused on how the buffer for my device driver works, and how to go about getting it to behave like /dev/random.

i made a test program to see how /dev/random works:

int test_read = open("/dev/random", O_RDONLY);
int get_random_num(void) {

  unsigned int random_num;
  read(test_read, &random_num, sizeof(random_num));
  return (random_num % 50) + 1;
}

and then this is my device driver’s read function(concised version):

my_random_read(struct file* file, char* buf,
                      size_t count, loff_t* ppos)
{
    unsigned int random_num; 
    get_random_bytes(&random_num, 1);
    int my_num = 1 + (random_num % 50)

    int* my_num_pointer = &my_num;

    copy_to_user(buf, my_num_pointer, count);
}

when i run my tester on QEMU i get -1074311964 : /

how can i make it so that all i would have to do is replace /dev/random with /dev/my_random??

this is how i’m reading from /dev/my_random:

    read(test_read, &random_number, sizeof(random_number), 0);
  • 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-06-01T05:47:28+00:00Added an answer on June 1, 2026 at 5:47 am
    unsigned int random_num; 
    get_random_bytes(&random_num, 1);
    

    You’re using get_random_bytes() incorrectly. The second parameter is the number of bytes, not words:

    void get_random_bytes(void *buf, int nbytes)
    

    You’re further returning private kernel memory to userspace:

    my_random_read(struct file* file, char* buf,
                          size_t count, loff_t* ppos)
    {
        /* ... */
        int* my_num_pointer = &my_num;
        copy_to_user(buf, my_num_pointer, count);
    }
    

    You’ve performed no validations on count — you only have one, maybe two, bytes of actual random data here. The rest of the data is garbage stack data and you’ve leaked it to userspace here. This is a security flaw — the kernel should not leak uninitialized data to userspace. (It might be garbage to rely on it yourself but an attacker that asks for ten pages of data from /dev/random should not get raw kernel memory.)

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

Sidebar

Related Questions

Do you have better way to make the device awakes by 100%? I repeatedly
Using this sample : I have make my own Fragment that holds tabhost and
how do i have make it working? class Device < ActiveRecord::Base has_many :prices has_many
I have to make an application that should capable of reading PDF documents on
I have a process that feeds a piece of hardware (data transmission device) with
I have make a new dropdown navi in css . Chrome, firefox, safari is
I have make a messaging system in which user can send messages to each
I have to make a graphical user interface application using the language of my
I have to make a connection to an XMLRPC site from a web application,
I have to make a JSP/HTML pages. How to fix height and width on

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.