Skip to content

Commit eb2c179

Browse files
Herbert Xuherbertx
authored andcommitted
crypto: rng - Fix extrng EFAULT handling
JIRA: https://issues.redhat.com/browse/RHEL-70652 Upstream Status: RHEL only When the iov_iter change was added the EFAULT handling became broken. Fix it by checking that copy_to_iter copied the correct number of bytes. Also make sure that a failure in crypto_rng_get_bytes does not cause previous bytes to be discarded. Finally update the scheduling code per get_random_bytes_user. Signed-off-by: Herbert Xu <herbert.xu@redhat.com>
1 parent 04ae76b commit eb2c179

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

crypto/rng.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,20 +288,21 @@ static ssize_t crypto_devrandom_read_iter(struct iov_iter *iter, bool reseed)
288288
i = min_t(size_t, iov_iter_count(iter), sizeof(tmp));
289289
err = crypto_rng_get_bytes(rng, tmp, i);
290290
if (err) {
291-
ret = err;
291+
ret = ret ?: err;
292292
break;
293293
}
294294

295295
copied = copy_to_iter(tmp, i, iter);
296296
ret += copied;
297297

298-
if (!iov_iter_count(iter))
298+
if (!iov_iter_count(iter) || copied != i)
299299
break;
300300

301-
if (need_resched()) {
301+
BUILD_BUG_ON(PAGE_SIZE % sizeof(tmp) != 0);
302+
if (ret % PAGE_SIZE == 0) {
302303
if (signal_pending(current))
303304
break;
304-
schedule();
305+
cond_resched();
305306
}
306307
}
307308

@@ -310,8 +311,7 @@ static ssize_t crypto_devrandom_read_iter(struct iov_iter *iter, bool reseed)
310311
else
311312
crypto_put_default_rng();
312313
memzero_explicit(tmp, sizeof(tmp));
313-
314-
return ret;
314+
return ret ? ret : -EFAULT;
315315
}
316316

317317
static const struct random_extrng crypto_devrandom_rng = {

0 commit comments

Comments
 (0)