|
19 | 19 | #include <sys/wait.h> |
20 | 20 | #include <linux/vm_sockets.h> |
21 | 21 |
|
| 22 | +#include <nsm.h> |
| 23 | + |
22 | 24 | #include "include/archive.h" |
23 | 25 | #include "include/fs_init.h" |
24 | 26 | #include "include/cgroups_init.h" |
|
30 | 32 | #define VSOCK_CID 3 |
31 | 33 | #define VSOCK_PORT 9000 |
32 | 34 |
|
| 35 | +#define NSM_PCR_ROOTFS 16 |
| 36 | +#define NSM_PCR_EXEC_ENV 17 |
| 37 | + |
| 38 | +#define NSM_PCR_DATA_SIZE 256 |
| 39 | + |
33 | 40 | /* |
34 | 41 | * Block or unblock signals. |
35 | 42 | * |
@@ -276,6 +283,56 @@ launch(char **argv, char **envp) |
276 | 283 | return 0; |
277 | 284 | } |
278 | 285 |
|
| 286 | +static int |
| 287 | +nsm_pcr_extend_rootfs_exec(void *rootfs, uint32_t rootfs_size, char *exec_path, |
| 288 | + char **exec_argv, char **exec_envp) |
| 289 | +{ |
| 290 | + uint8_t pcr_data[NSM_PCR_DATA_SIZE]; |
| 291 | + uint32_t pcr_data_size; |
| 292 | + int ret, nsm_fd; |
| 293 | + char *c; |
| 294 | + |
| 295 | + nsm_fd = nsm_lib_init(); |
| 296 | + if (nsm_fd < 0) |
| 297 | + return -1; |
| 298 | + |
| 299 | + ret = nsm_extend_pcr(nsm_fd, NSM_PCR_ROOTFS, (uint8_t *) rootfs, |
| 300 | + rootfs_size, pcr_data, &pcr_data_size); |
| 301 | + if (ret != ERROR_CODE_SUCCESS) |
| 302 | + return -ret; |
| 303 | + |
| 304 | + ret = nsm_lock_pcr(nsm_fd, NSM_PCR_ROOTFS); |
| 305 | + if (ret != ERROR_CODE_SUCCESS) |
| 306 | + return -ret; |
| 307 | + |
| 308 | + ret = nsm_extend_pcr(nsm_fd, NSM_PCR_EXEC_ENV, (uint8_t *) exec_path, |
| 309 | + strlen(exec_path), pcr_data, &pcr_data_size); |
| 310 | + if (ret != ERROR_CODE_SUCCESS) |
| 311 | + return -ret; |
| 312 | + |
| 313 | + for (int i = 0; (c = exec_argv[i]) != NULL; ++i) { |
| 314 | + ret = nsm_extend_pcr(nsm_fd, NSM_PCR_EXEC_ENV, (uint8_t *) c, strlen(c), |
| 315 | + pcr_data, &pcr_data_size); |
| 316 | + if (ret != ERROR_CODE_SUCCESS) |
| 317 | + return -ret; |
| 318 | + } |
| 319 | + |
| 320 | + for (int i = 0; (c = exec_envp[i]) != NULL; ++i) { |
| 321 | + ret = nsm_extend_pcr(nsm_fd, NSM_PCR_EXEC_ENV, (uint8_t *) c, strlen(c), |
| 322 | + pcr_data, &pcr_data_size); |
| 323 | + if (ret != ERROR_CODE_SUCCESS) |
| 324 | + return -ret; |
| 325 | + } |
| 326 | + |
| 327 | + ret = nsm_lock_pcr(nsm_fd, NSM_PCR_EXEC_ENV); |
| 328 | + if (ret != ERROR_CODE_SUCCESS) |
| 329 | + return -ret; |
| 330 | + |
| 331 | + nsm_lib_exit(nsm_fd); |
| 332 | + |
| 333 | + return 0; |
| 334 | +} |
| 335 | + |
279 | 336 | int |
280 | 337 | main(int argc, char *argv[]) |
281 | 338 | { |
@@ -323,6 +380,11 @@ main(int argc, char *argv[]) |
323 | 380 | if (ret < 0) |
324 | 381 | exit(ret); |
325 | 382 |
|
| 383 | + ret = nsm_pcr_extend_rootfs_exec(rootfs_archive, archive_size, exec_path, |
| 384 | + exec_argv, exec_envp); |
| 385 | + if (ret < 0) |
| 386 | + exit(ret); |
| 387 | + |
326 | 388 | ret = rootfs_mount(); |
327 | 389 | if (ret < 0) |
328 | 390 | exit(ret); |
|
0 commit comments