Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit db33f31

Browse files
committed
fix possible access memory after free
For tasks created by clone_VM, they share the memory with parent, so when parent frees the stack, children may still access to this stack. so don't free stack when children is still using the stack. Signed-off-by: Gao feng <[email protected]>
1 parent e6cffd9 commit db33f31

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/exec.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,6 @@ static int hyper_do_exec_cmd(void *data)
430430

431431
if (hyper_get_type(pipe[0], &type) < 0 || type != READY) {
432432
fprintf(stderr, "hyper init doesn't get execcmd ready message\n");
433-
hyper_send_type(arg->pipe[1], ERROR);
434433
goto out;
435434
}
436435

@@ -461,13 +460,14 @@ static int hyper_do_exec_cmd(void *data)
461460

462461
ret = 0;
463462
exit:
463+
close(pipe[0]);
464+
close(pipe[1]);
464465
hyper_send_type(pipe[1], ERROR);
465466
_exit(ret);
466-
467467
out:
468-
hyper_send_type(arg->pipe[1], ret ? ERROR : READY);
469468
close(pipe[0]);
470469
close(pipe[1]);
470+
hyper_send_type(arg->pipe[1], ret ? ERROR : READY);
471471
_exit(ret);
472472
}
473473

@@ -534,7 +534,6 @@ int hyper_exec_cmd(char *json, int length)
534534

535535
pid = clone(hyper_do_exec_cmd, stack + stacksize, CLONE_VM| CLONE_FILES| SIGCHLD, &arg);
536536
fprintf(stdout, "do_exec_cmd pid %d\n", pid);
537-
free(stack);
538537
if (pid < 0) {
539538
perror("clone hyper_do_exec_cmd failed");
540539
goto close_tty;
@@ -550,6 +549,7 @@ int hyper_exec_cmd(char *json, int length)
550549
out:
551550
close(arg.pipe[0]);
552551
close(arg.pipe[1]);
552+
free(stack);
553553
return ret;
554554
close_tty:
555555
close(exec->ptyfd);

src/init.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,13 +402,14 @@ static int hyper_container_stage0(void *data)
402402

403403
ret = hyper_start_container(c, utsns, ipcns, pod);
404404
out:
405+
close(pidns);
406+
close(utsns);
407+
close(ipcns);
408+
405409
if (hyper_send_type(arg->ctl_pipe[1], ret ? ERROR : READY) < 0) {
406410
fprintf(stderr, "container init send ready message failed\n");
407411
}
408412

409-
close(pidns);
410-
close(utsns);
411-
close(ipcns);
412413
/* hyper_container_stage0 shares fd table with init, let init closes pipe. */
413414
//close(arg->ctl_pipe[0]);
414415
//close(arg->ctl_pipe[1]);
@@ -440,7 +441,6 @@ int hyper_start_container_stage0(struct hyper_container *c, struct hyper_pod *po
440441
}
441442

442443
pid = clone(hyper_container_stage0, stack + stacksize, CLONE_VM| CLONE_FILES| SIGCHLD, &arg);
443-
free(stack);
444444
if (pid < 0) {
445445
perror("enter container pid ns failed");
446446
goto out;
@@ -464,6 +464,8 @@ int hyper_start_container_stage0(struct hyper_container *c, struct hyper_pod *po
464464
out:
465465
close(arg.ctl_pipe[0]);
466466
close(arg.ctl_pipe[1]);
467+
468+
free(stack);
467469
return ret;
468470
}
469471

@@ -916,7 +918,7 @@ static int hyper_do_cmd_read_file(void *data)
916918
ret = 0;
917919
err:
918920
hyper_send_type(arg->pipe[1], ret ? ERROR : READY);
919-
return ret;
921+
_exit(ret);
920922
}
921923

922924
static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_t **data)
@@ -967,7 +969,6 @@ static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_
967969
}
968970

969971
pid = clone(hyper_do_cmd_read_file, stack + stacksize, CLONE_VM| SIGCHLD, &arg);
970-
free(stack);
971972
if (pid < 0) {
972973
perror("fail to fork writter process");
973974
goto out;
@@ -984,6 +985,7 @@ static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_
984985
close(arg.pipe[1]);
985986
free(reader.id);
986987
free(reader.file);
988+
free(stack);
987989

988990
return ret;
989991
}

0 commit comments

Comments
 (0)