Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPHINCS+ memcpy source and destination overlap - undefined behavior #1038

Closed
bhess opened this issue Jun 29, 2021 · 4 comments
Closed

SPHINCS+ memcpy source and destination overlap - undefined behavior #1038

bhess opened this issue Jun 29, 2021 · 4 comments

Comments

@bhess
Copy link
Member

bhess commented Jun 29, 2021

The SPHINCS+ implementations have cases of memcpy use where the source and destination overlap. According to the C standard and posix, memcpy behavior is undefined if memory regions overlap.

Detected using valgrind on ppc64le/Ubuntu focal. Memcpy implementations vary, so it seems to be not detected with valgrind on x86_64.

The cause in gen_chain:

static void gen_chain(unsigned char *out, const unsigned char *in,
unsigned int start, unsigned int steps,
const unsigned char *pub_seed, uint32_t addr[8],
const hash_state *hash_state_seeded) {
uint32_t i;
/* Initialize out with the value at position 'start'. */
memcpy(out, in, PQCLEAN_SPHINCSHARAKA128FROBUST_CLEAN_N);

Used for example by wots_gen_pk, where src and dst are the same. The replicated code of all variants is affected:

gen_chain(pk + i * PQCLEAN_SPHINCSHARAKA128FROBUST_CLEAN_N, pk + i * PQCLEAN_SPHINCSHARAKA128FROBUST_CLEAN_N,
0, PQCLEAN_SPHINCSHARAKA128FROBUST_CLEAN_WOTS_W - 1, pub_seed, addr, hash_state_seeded);

Using memmove would be the safe alternative, or avoid memcpy if src and dst are the same.

Below is the valgrind log. It's part of a constant-time check, but the issues detected are because of overlapping memory.
ppc64le.txt

@baentsch
Copy link
Member

Yikes -- good catch! Hard to believe the upstreams didn't encounter this before, at least PQClean: Shouldn't this issue then rather be opened there?

@bhess
Copy link
Member Author

bhess commented Jun 29, 2021

PQClean: Shouldn't this issue then rather be opened there?

Yes, thanks. Created an issue in PQClean that links to the one here.

@baentsch
Copy link
Member

Is this still an issue with the new Sphincs+ code or can this be closed by now?

@bhess
Copy link
Member Author

bhess commented Jul 10, 2023

I checked and it appears to not be an issue anymore with the current version. Closing.

@bhess bhess closed this as completed Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

No branches or pull requests

2 participants