Regarding getrandom syscall

Avantika Rawat avani.rawat at gmail.com
Wed Jun 22 12:40:27 EDT 2016


Hi

I have already tried that value by checking in forums but got the same
error.
On Jun 22, 2016 9:46 PM, "Navin P.S" <navinp1912 at gmail.com> wrote:

> On Wed, Jun 22, 2016 at 8:28 PM, Avantika Rawat <avani.rawat at gmail.com>
> wrote:
> > Hi ,
> >
> > Thanks for the link but i am getting "Function not implemented" error.
> > I have added a custom syscall in kernel for testing, getting similar
> > error for that also. I am working on  3.10.20 Kernel and MIPS
> > architecture. Pasting the kernel changes done to spport getrandom
> > syscall in 3.10.20 kernel. Can someone please help me here that what
> > needs to be done.
> >
> > --- linux-3.10.20/arch/x86/syscalls/syscall_32.tbl 2016-06-15
> > 11:51:27.950025308 +0530
> > +++ linux-3.10.20_mod/arch/x86/syscalls/syscall_32.tbl 2016-06-15
> > 11:48:02.222020102 +0530
> > @@ -358,3 +358,4 @@
> >  349 i386 kcmp sys_kcmp
> >  350 i386 finit_module sys_finit_module
> >  351 i386 msa sys_msa
> > +352 i386    getrandom           sys_getrandom
> > --- linux-3.10.20/arch/x86/syscalls/syscall_64.tbl 2016-06-15
> > 11:51:27.950025308 +0530
> > +++ linux-3.10.20_mod/arch/x86/syscalls/syscall_64.tbl 2016-06-15
> > 11:48:16.118020453 +0530
> > @@ -321,6 +321,7 @@
> >  312 common kcmp sys_kcmp
> >  313 common finit_module sys_finit_module
> >  314 common msa sys_msa
> > +315 common getrandom sys_getrandom
> >
> >  #
> >  # x32-specific system call numbers start at 512 to avoid cache impact
> > --- linux-3.10.20/drivers/char/random.c 2016-06-15 11:51:27.998025309
> +0530
> > +++ linux-3.10.20_mod/drivers/char/random.c 2016-06-15
> 11:48:34.774020925 +0530
> > @@ -265,6 +265,8 @@
> >  #include <asm/irq.h>
> >  #include <asm/irq_regs.h>
> >  #include <asm/io.h>
> > +#include <linux/syscalls.h>
> > +#include <linux/completion.h>
> >
> >  #define CREATE_TRACE_POINTS
> >  #include <trace/events/random.h>
> > @@ -397,6 +399,7 @@ static struct poolinfo {
> >   */
> >  static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);
> >  static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);
> > +static DECLARE_WAIT_QUEUE_HEAD(urandom_init_wait);
> >  static struct fasync_struct *fasync;
> >
> >  static bool debug;
> > @@ -1018,13 +1021,14 @@ static ssize_t extract_entropy_user(stru
> >  {
> >   ssize_t ret = 0, i;
> >   __u8 tmp[EXTRACT_SIZE];
> > +    int large_request = (nbytes > 256);
> >
> >   trace_extract_entropy_user(r->name, nbytes, r->entropy_count,
> _RET_IP_);
> >   xfer_secondary_pool(r, nbytes);
> >   nbytes = account(r, nbytes, 0, 0);
> >
> >   while (nbytes) {
> > - if (need_resched()) {
> > +        if (large_request && need_resched()) {
> >   if (signal_pending(current)) {
> >   if (ret == 0)
> >   ret = -ERESTARTSYS;
> > @@ -1158,7 +1162,7 @@ void rand_initialize_disk(struct gendisk
> >  #endif
> >
> >  static ssize_t
> > -random_read(struct file *file, char __user *buf, size_t nbytes, loff_t
> *ppos)
> > +_random_read(int nonblock, char __user *buf, size_t nbytes)
> >  {
> >   ssize_t n, retval = 0, count = 0;
> >
> > @@ -1183,7 +1187,7 @@ random_read(struct file *file, char __us
> >    n*8, (nbytes-n)*8);
> >
> >   if (n == 0) {
> > - if (file->f_flags & O_NONBLOCK) {
> > + if (nonblock) {
> >   retval = -EAGAIN;
> >   break;
> >   }
> > @@ -1215,6 +1219,13 @@ random_read(struct file *file, char __us
> >  }
> >
> >  static ssize_t
> > +random_read(struct file *file, char __user *buf, size_t nbytes, loff_t
> *ppos)
> > +    {
> > +    return _random_read(file->f_flags & O_NONBLOCK, buf, nbytes);
> > +    }
> > +
> > +
> > +static ssize_t
> >  urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t
> *ppos)
> >  {
> >   return extract_entropy_user(&nonblocking_pool, buf, nbytes);
> > @@ -1340,6 +1351,29 @@ const struct file_operations urandom_fop
> >   .llseek = noop_llseek,
> >  };
> >
> > +SYSCALL_DEFINE3 (getrandom, char __user *, buf, size_t, count,
> > + unsigned int, flags)
> > +    {
> > +    if (flags & ~(GRND_NONBLOCK|GRND_RANDOM))
> > +        return -EINVAL;
> > +
> > +    if (count > INT_MAX)
> > +        count = INT_MAX;
> > +
> > +    if (flags & GRND_RANDOM)
> > +        return _random_read(flags & GRND_NONBLOCK, buf, count);
> > +
> > +    if (unlikely(nonblocking_pool.initialized == 0)) {
> > +        if (flags & GRND_NONBLOCK)
> > +            return -EAGAIN;
> > +        wait_event_interruptible(urandom_init_wait,
> > +                                 nonblocking_pool.initialized);
> > +        if (signal_pending(current))
> > +            return -ERESTARTSYS;
> > +    }
> > +    return urandom_read(NULL, buf, count, NULL);
> > +    }
> > +
> >  /***************************************************************
> >   * Random UUID interface
> >   *
> > --- linux-3.10.20/include/linux/syscalls.h 2016-06-15 11:51:28.246025315
> +0530
> > +++ linux-3.10.20_mod/include/linux/syscalls.h 2016-06-15
> > 11:49:11.918021865 +0530
> > @@ -852,4 +852,6 @@ asmlinkage long sys_process_vm_writev(pi
> >  asmlinkage long sys_kcmp(pid_t pid1, pid_t pid2, int type,
> >   unsigned long idx1, unsigned long idx2);
> >  asmlinkage long sys_finit_module(int fd, const char __user *uargs, int
> flags);
> > +asmlinkage long sys_getrandom(char __user *buf, size_t count,
> > +      unsigned int flags);
> >  #endif
> > --- linux-3.10.20/include/uapi/asm-generic/unistd.h 2016-06-15
> > 11:51:28.258025315 +0530
> > +++ linux-3.10.20_mod/include/uapi/asm-generic/unistd.h 2016-06-15
> > 11:49:32.558022388 +0530
> > @@ -694,9 +694,11 @@ __SYSCALL(__NR_kcmp, sys_kcmp)
> >  __SYSCALL(__NR_finit_module, sys_finit_module)
> >  #define __NR_msa 274
> >  __SYSCALL(__NR_msa, sys_msa)
> > +#define __NR_getrandom 275
> > +__SYSCALL(__NR_getrandom, sys_getrandom)
> >
> >  #undef __NR_syscalls
> > -#define __NR_syscalls 275
> > +#define __NR_syscalls 276
> >
> >  /*
> >   * All syscalls below here should go away really,
> > --- linux-3.10.20/include/uapi/linux/random.h 2013-11-21
> > 01:58:01.000000000 +0530
> > +++ linux-3.10.20_mod/include/uapi/linux/random.h 2016-06-15
> > 11:50:06.350023243 +0530
> > @@ -40,6 +40,15 @@ struct rand_pool_info {
> >   __u32 buf[0];
> >  };
> >
> > +/*
> > + * Flags for getrandom(2)
> > + *
> > + * GRND_NONBLOCK Don't block and return EAGAIN instead
> > + * GRND_RANDOM Use the /dev/random pool instead of /dev/urandom
> > + */
> > +#define GRND_NONBLOCK 0x0001
> > +#define GRND_RANDOM 0x0002
> > +
> >  struct rnd_state {
> >   __u32 s1, s2, s3;
> >  };
> >
> >
> > On Wed, Jun 22, 2016 at 12:40 AM, Manuel Pégourié-Gonnard
> > <mpg at elzevir.fr> wrote:
> >> Hi,
> >>
> >> Maybe your unistd.h comes from an older kernel? That would explain why
> >> it doesn't define that symbol.
> >>
> >> Here [1] is some code using that syscall successfully, you can see it
> >> looks a lot like Sayutin's code, except it does a few more compile-time
> >> and runtime checks to fallback on using /dev/urandom if necessary.
> >>
> >> [1]:
> >>
> https://github.com/ARMmbed/mbedtls/blob/dd9895d8101f17ce804830472cbb140eba1c46a0/library/entropy_poll.c#L85
> >>
> >> Manuel.
> >>
> >>
> >> On 21/06/2016 15:41, Avantika Rawat wrote:
> >>> Hi,
> >>>
> >>> Thanks for the suggestion but i am getting following error
> >>>
> >>>  error: 'SYS_getrandom' undeclared (first use in this function)
> >>>
> >>> then i have replaced the SYS_getrandom by 275 in syscall(275, --
> args--);
> >>> as 275 is the  __NR_getrandom defines in unistd.h file,  but it is
> >>> returning -1.
> >>> Can somebody tell me what i am missing here??
> >>>
> >>>  . Also in /proc/kallsyms it is showing two syscalls added for
> getrandom
> >>>
> >>> ffffffff80417a38 T SyS_getrandom
> >>> ffffffff80417a38 T sys_getrandom
> >>>
> >>> On Sat, Jun 18, 2016 at 7:12 PM, Sayutin Dmitry <cdkrot at yandex.ru>
> wrote:
> >>>> Well, in fact it is not hard.
> >>>>
> >>>> Just use syscall(2) provided by libc.
> >>>>
> >>>> You need to provide to this function syscall id and syscall args.
> >>>> Syscall id can be retrieved from macro constant
> >>>> Should look something like:
> >>>>
> >>>> #define _GNU_SOURCE
> >>>> #include <unistd.h>
> >>>> #include <sys/syscall.h>
> >>>>
> >>>> syscall(SYS_getrandom, -- your - args - here --);
> >>>>
> >>>> It returns long value - the result of syscall.
> >>>> If it is between [-4095; -1] then it is negated errno, otherways it
> is return value.
> >>>>
> >>>>
> >>>> 18.06.2016, 16:32, "Anoop" <anoop.chargotra at gmail.com>:
> >>>>> Hi Avantika,
> >>>>>
> >>>>> On Sat, Jun 18, 2016 at 5:32 PM, Avantika Rawat <
> avani.rawat at gmail.com> wrote:
> >>>>>>  Hi ALL,
> >>>>>>
> >>>>>>  I am trying to use getrandom syscall in kernel 3.10.20 by
> following this
> >>>>>>  link
> >>>>>>
> >>>>>>
> https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=c6e9d6f38894798696f23c8084ca7edbf16ee895
> >>>>>>
> >>>>>>  i have compiled the kernel and now want to call the getrandom
> syscall from
> >>>>>>  userspace to generate random numbers. But i am getting following
> error while
> >>>>>>  calling the getrandom () from userspace.
> >>>>>>
> >>>>>>  (.text.startup+0x18): undefined reference to `getrandom'
> >>>>>>  (.text.startup+0x1c): undefined reference to `getrandom'
> >>>>>
> >>>>> Your user space program will not know where 'getrandom' is defined
> >>>>> unless it's in the C library. You need to research more on how to
> call
> >>>>> custom system calls.
> >>>>>
> >>>>> -Anoop
> >>>>>
> >>>>>>  --
> >>>>>>  Regards,
> >>>>>>  Avantika Rawat
> >>>>>>
> >>>>>>  _______________________________________________
> >>>>>>  Kernelnewbies mailing list
> >>>>>>  Kernelnewbies at kernelnewbies.org
> >>>>>>  http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
> >>>>>
> >>>>> _______________________________________________
> >>>>> Kernelnewbies mailing list
> >>>>> Kernelnewbies at kernelnewbies.org
> >>>>> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
> >>>>
> >>>> -----
> >>>> Sayutin Dmitry <cdkrot at yandex.com>
> >>>
> >>>
> >>>
> >
> >
> >
> > --
> > Regards,
> > Avantika Rawat
> >
> > _______________________________________________
> > Kernelnewbies mailing list
> > Kernelnewbies at kernelnewbies.org
> > http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
> I think you need to 352 or 315 ie the values from .tbl (so 315 if you
> are calling on a x64 kernel or 352 if you are calling x86 kernel). The
> 275 is for the uapi but syscall accepts the 315 value on a x64.
>
>  Please try with 315 and 352 after running your modified kernel and
> report back. Atleast that is the way it worked for me on a 4.x kernel.
> Ideally uapi is for userspace like glibc and they come man 3 where as
> the syscall is (man 2) ie system call.
>
> --
> -- Navin
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20160622/da7ccc23/attachment-0001.html 


More information about the Kernelnewbies mailing list