Commit e8a533cb authored by Jason A. Donenfeld's avatar Jason A. Donenfeld
Browse files

treewide: use get_random_u32_inclusive() when possible



These cases were done with this Coccinelle:

@@
expression H;
expression L;
@@
- (get_random_u32_below(H) + L)
+ get_random_u32_inclusive(L, H + L - 1)

@@
expression H;
expression L;
expression E;
@@
  get_random_u32_inclusive(L,
  H
- + E
- - E
  )

@@
expression H;
expression L;
expression E;
@@
  get_random_u32_inclusive(L,
  H
- - E
- + E
  )

@@
expression H;
expression L;
expression E;
expression F;
@@
  get_random_u32_inclusive(L,
  H
- - E
  + F
- + E
  )

@@
expression H;
expression L;
expression E;
expression F;
@@
  get_random_u32_inclusive(L,
  H
- + E
  + F
- - E
  )

And then subsequently cleaned up by hand, with several automatic cases
rejected if it didn't make sense contextually.

Reviewed-by: default avatarKees Cook <keescook@chromium.org>
Reviewed-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> # for infiniband
Signed-off-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
parent d247aabd
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ static unsigned long int get_module_load_offset(void)
		 */
		if (module_load_offset == 0)
			module_load_offset =
				(get_random_u32_below(1024) + 1) * PAGE_SIZE;
				get_random_u32_inclusive(1, 1024) * PAGE_SIZE;
		mutex_unlock(&module_kaslr_mutex);
	}
	return module_load_offset;
+1 −1
Original line number Diff line number Diff line
@@ -253,7 +253,7 @@ static int pkcs1pad_encrypt(struct akcipher_request *req)
	ps_end = ctx->key_size - req->src_len - 2;
	req_ctx->in_buf[0] = 0x02;
	for (i = 1; i < ps_end; i++)
		req_ctx->in_buf[i] = 1 + get_random_u32_below(255);
		req_ctx->in_buf[i] = get_random_u32_inclusive(1, 255);
	req_ctx->in_buf[ps_end] = 0x00;

	pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
+5 −5
Original line number Diff line number Diff line
@@ -962,11 +962,11 @@ static char *generate_random_sgl_divisions(struct test_sg_division *divs,
		if (div == &divs[max_divs - 1] || get_random_u32_below(2) == 0)
			this_len = remaining;
		else
			this_len = 1 + get_random_u32_below(remaining);
			this_len = get_random_u32_inclusive(1, remaining);
		div->proportion_of_total = this_len;

		if (get_random_u32_below(4) == 0)
			div->offset = (PAGE_SIZE - 128) + get_random_u32_below(128);
			div->offset = get_random_u32_inclusive(PAGE_SIZE - 128, PAGE_SIZE - 1);
		else if (get_random_u32_below(2) == 0)
			div->offset = get_random_u32_below(32);
		else
@@ -1094,12 +1094,12 @@ static void generate_random_testvec_config(struct testvec_config *cfg,
	}

	if (get_random_u32_below(2) == 0) {
		cfg->iv_offset = 1 + get_random_u32_below(MAX_ALGAPI_ALIGNMASK);
		cfg->iv_offset = get_random_u32_inclusive(1, MAX_ALGAPI_ALIGNMASK);
		p += scnprintf(p, end - p, " iv_offset=%u", cfg->iv_offset);
	}

	if (get_random_u32_below(2) == 0) {
		cfg->key_offset = 1 + get_random_u32_below(MAX_ALGAPI_ALIGNMASK);
		cfg->key_offset = get_random_u32_inclusive(1, MAX_ALGAPI_ALIGNMASK);
		p += scnprintf(p, end - p, " key_offset=%u", cfg->key_offset);
	}

@@ -1653,7 +1653,7 @@ static void generate_random_hash_testvec(struct shash_desc *desc,
	if (maxkeysize) {
		vec->ksize = maxkeysize;
		if (get_random_u32_below(4) == 0)
			vec->ksize = 1 + get_random_u32_below(maxkeysize);
			vec->ksize = get_random_u32_inclusive(1, maxkeysize);
		generate_random_bytes((u8 *)vec->key, vec->ksize);

		vec->setkey_error = crypto_shash_setkey(desc->tfm, vec->key,
+1 −1
Original line number Diff line number Diff line
@@ -129,7 +129,7 @@ enum mhi_pm_state {
#define PRIMARY_CMD_RING				0
#define MHI_DEV_WAKE_DB					127
#define MHI_MAX_MTU					0xffff
#define MHI_RANDOM_U32_NONZERO(bmsk)			(get_random_u32_below(bmsk) + 1)
#define MHI_RANDOM_U32_NONZERO(bmsk)			(get_random_u32_inclusive(1, bmsk))

enum mhi_er_type {
	MHI_ER_TYPE_INVALID = 0x0,
+1 −1
Original line number Diff line number Diff line
@@ -400,7 +400,7 @@ static int __find_race(void *arg)
		struct dma_fence *fence = dma_fence_get(data->fc.tail);
		int seqno;

		seqno = get_random_u32_below(data->fc.chain_length) + 1;
		seqno = get_random_u32_inclusive(1, data->fc.chain_length);

		err = dma_fence_chain_find_seqno(&fence, seqno);
		if (err) {
Loading