@@ -397,13 +397,13 @@ void get_random_bytes(void *buf, size_t len)
397397}
398398EXPORT_SYMBOL (get_random_bytes );
399399
400- static ssize_t get_random_bytes_user (void __user * ubuf , size_t len )
400+ static ssize_t get_random_bytes_user (struct iov_iter * iter )
401401{
402- size_t block_len , left , ret = 0 ;
403402 u32 chacha_state [CHACHA_STATE_WORDS ];
404- u8 output [CHACHA_BLOCK_SIZE ];
403+ u8 block [CHACHA_BLOCK_SIZE ];
404+ size_t ret = 0 , copied ;
405405
406- if (! len )
406+ if (unlikely (! iov_iter_count ( iter )) )
407407 return 0 ;
408408
409409 /*
@@ -417,38 +417,30 @@ static ssize_t get_random_bytes_user(void __user *ubuf, size_t len)
417417 * use chacha_state after, so we can simply return those bytes to
418418 * the user directly.
419419 */
420- if (len <= CHACHA_KEY_SIZE ) {
421- ret = len - copy_to_user ( ubuf , & chacha_state [4 ], len );
420+ if (iov_iter_count ( iter ) <= CHACHA_KEY_SIZE ) {
421+ ret = copy_to_iter ( & chacha_state [4 ], CHACHA_KEY_SIZE , iter );
422422 goto out_zero_chacha ;
423423 }
424424
425425 for (;;) {
426- chacha20_block (chacha_state , output );
426+ chacha20_block (chacha_state , block );
427427 if (unlikely (chacha_state [12 ] == 0 ))
428428 ++ chacha_state [13 ];
429429
430- block_len = min_t (size_t , len , CHACHA_BLOCK_SIZE );
431- left = copy_to_user (ubuf , output , block_len );
432- if (left ) {
433- ret += block_len - left ;
434- break ;
435- }
436-
437- ubuf += block_len ;
438- ret += block_len ;
439- len -= block_len ;
440- if (!len )
430+ copied = copy_to_iter (block , sizeof (block ), iter );
431+ ret += copied ;
432+ if (!iov_iter_count (iter ) || copied != sizeof (block ))
441433 break ;
442434
443- BUILD_BUG_ON (PAGE_SIZE % CHACHA_BLOCK_SIZE != 0 );
435+ BUILD_BUG_ON (PAGE_SIZE % sizeof ( block ) != 0 );
444436 if (ret % PAGE_SIZE == 0 ) {
445437 if (signal_pending (current ))
446438 break ;
447439 cond_resched ();
448440 }
449441 }
450442
451- memzero_explicit (output , sizeof (output ));
443+ memzero_explicit (block , sizeof (block ));
452444out_zero_chacha :
453445 memzero_explicit (chacha_state , sizeof (chacha_state ));
454446 return ret ? ret : - EFAULT ;
@@ -1248,6 +1240,10 @@ static void __cold try_to_generate_entropy(void)
12481240
12491241SYSCALL_DEFINE3 (getrandom , char __user * , ubuf , size_t , len , unsigned int , flags )
12501242{
1243+ struct iov_iter iter ;
1244+ struct iovec iov ;
1245+ int ret ;
1246+
12511247 if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE ))
12521248 return - EINVAL ;
12531249
@@ -1258,19 +1254,18 @@ SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int, flags
12581254 if ((flags & (GRND_INSECURE | GRND_RANDOM )) == (GRND_INSECURE | GRND_RANDOM ))
12591255 return - EINVAL ;
12601256
1261- if (len > INT_MAX )
1262- len = INT_MAX ;
1263-
12641257 if (!crng_ready () && !(flags & GRND_INSECURE )) {
1265- int ret ;
1266-
12671258 if (flags & GRND_NONBLOCK )
12681259 return - EAGAIN ;
12691260 ret = wait_for_random_bytes ();
12701261 if (unlikely (ret ))
12711262 return ret ;
12721263 }
1273- return get_random_bytes_user (ubuf , len );
1264+
1265+ ret = import_single_range (READ , ubuf , len , & iov , & iter );
1266+ if (unlikely (ret ))
1267+ return ret ;
1268+ return get_random_bytes_user (& iter );
12741269}
12751270
12761271static __poll_t random_poll (struct file * file , poll_table * wait )
@@ -1314,8 +1309,7 @@ static ssize_t random_write(struct file *file, const char __user *ubuf,
13141309 return (ssize_t )len ;
13151310}
13161311
1317- static ssize_t urandom_read (struct file * file , char __user * ubuf ,
1318- size_t len , loff_t * ppos )
1312+ static ssize_t urandom_read_iter (struct kiocb * kiocb , struct iov_iter * iter )
13191313{
13201314 static int maxwarn = 10 ;
13211315
@@ -1331,23 +1325,22 @@ static ssize_t urandom_read(struct file *file, char __user *ubuf,
13311325 ++ urandom_warning .missed ;
13321326 else if (ratelimit_disable || __ratelimit (& urandom_warning )) {
13331327 -- maxwarn ;
1334- pr_notice ("%s: uninitialized urandom read (%zd bytes read)\n" ,
1335- current -> comm , len );
1328+ pr_notice ("%s: uninitialized urandom read (%zu bytes read)\n" ,
1329+ current -> comm , iov_iter_count ( iter ) );
13361330 }
13371331 }
13381332
1339- return get_random_bytes_user (ubuf , len );
1333+ return get_random_bytes_user (iter );
13401334}
13411335
1342- static ssize_t random_read (struct file * file , char __user * ubuf ,
1343- size_t len , loff_t * ppos )
1336+ static ssize_t random_read_iter (struct kiocb * kiocb , struct iov_iter * iter )
13441337{
13451338 int ret ;
13461339
13471340 ret = wait_for_random_bytes ();
13481341 if (ret != 0 )
13491342 return ret ;
1350- return get_random_bytes_user (ubuf , len );
1343+ return get_random_bytes_user (iter );
13511344}
13521345
13531346static long random_ioctl (struct file * f , unsigned int cmd , unsigned long arg )
@@ -1409,7 +1402,7 @@ static int random_fasync(int fd, struct file *filp, int on)
14091402}
14101403
14111404const struct file_operations random_fops = {
1412- .read = random_read ,
1405+ .read_iter = random_read_iter ,
14131406 .write = random_write ,
14141407 .poll = random_poll ,
14151408 .unlocked_ioctl = random_ioctl ,
@@ -1419,7 +1412,7 @@ const struct file_operations random_fops = {
14191412};
14201413
14211414const struct file_operations urandom_fops = {
1422- .read = urandom_read ,
1415+ .read_iter = urandom_read_iter ,
14231416 .write = random_write ,
14241417 .unlocked_ioctl = random_ioctl ,
14251418 .compat_ioctl = compat_ptr_ioctl ,
0 commit comments