Skip to content

Commit 03cdfaa

Browse files
nicstangeherbertx
authored andcommitted
lib/mpi: mpi_read_from_buffer(): return error code
mpi_read_from_buffer() reads a MPI from a buffer into a newly allocated MPI instance. It expects the buffer's leading two bytes to contain the number of bits, followed by the actual payload. On failure, it returns NULL and updates the in/out argument ret_nread somewhat inconsistently: - If the given buffer is too short to contain the leading two bytes encoding the number of bits or their value is unsupported, then ret_nread will be cleared. - If the allocation of the resulting MPI instance fails, ret_nread is left as is. The only user of mpi_read_from_buffer(), digsig_verify_rsa(), simply checks for a return value of NULL and returns -ENOMEM if that happens. While this is all of cosmetic nature only, there is another error condition which currently isn't detectable by the caller of mpi_read_from_buffer(): if the given buffer is too small to hold the number of bits as encoded in its first two bytes, the return value will be non-NULL and *ret_nread > 0. In preparation of communicating this condition to the caller, let mpi_read_from_buffer() return error values by means of the ERR_PTR() mechanism. Make the sole caller of mpi_read_from_buffer(), digsig_verify_rsa(), check the return value for IS_ERR() rather than == NULL. If IS_ERR() is true, return the associated error value rather than the fixed -ENOMEM. Signed-off-by: Nicolai Stange <nicstange@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent eef0df6 commit 03cdfaa

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

‎lib/digsig.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,18 @@ static int digsig_verify_rsa(struct key *key,
104104
datap = pkh->mpi;
105105
endp = ukp->data + ukp->datalen;
106106

107-
err = -ENOMEM;
108-
109107
for (i = 0; i < pkh->nmpi; i++) {
110108
unsigned int remaining = endp - datap;
111109
pkey[i] = mpi_read_from_buffer(datap, &remaining);
112-
if (!pkey[i])
110+
if (IS_ERR(pkey[i])) {
111+
err = PTR_ERR(pkey[i]);
113112
goto err;
113+
}
114114
datap += remaining;
115115
}
116116

117+
err = -ENOMEM;
118+
117119
mblen = mpi_get_nbits(pkey[0]);
118120
mlen = DIV_ROUND_UP(mblen, 8);
119121

@@ -126,8 +128,10 @@ static int digsig_verify_rsa(struct key *key,
126128

127129
nret = siglen;
128130
in = mpi_read_from_buffer(sig, &nret);
129-
if (!in)
131+
if (IS_ERR(in)) {
132+
err = PTR_ERR(in);
130133
goto err;
134+
}
131135

132136
res = mpi_alloc(mpi_get_nlimbs(in) * 2);
133137
if (!res)

‎lib/mpi/mpicoder.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
8686
MPI val = NULL;
8787

8888
if (*ret_nread < 2)
89-
goto leave;
89+
return ERR_PTR(-EINVAL);
9090
nbits = buffer[0] << 8 | buffer[1];
9191

9292
if (nbits > MAX_EXTERN_MPI_BITS) {
9393
pr_info("MPI: mpi too large (%u bits)\n", nbits);
94-
goto leave;
94+
return ERR_PTR(-EINVAL);
9595
}
9696
buffer += 2;
9797
nread = 2;
@@ -100,7 +100,7 @@ MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
100100
nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
101101
val = mpi_alloc(nlimbs);
102102
if (!val)
103-
return NULL;
103+
return ERR_PTR(-ENOMEM);
104104
i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
105105
i %= BYTES_PER_MPI_LIMB;
106106
val->nbits = nbits;

0 commit comments

Comments
 (0)