Skip to content
This repository has been archived by the owner on Feb 19, 2023. It is now read-only.

Commit

Permalink
denying writes to executables
Browse files Browse the repository at this point in the history
  • Loading branch information
CCXXXI committed Jan 10, 2021
1 parent b3cf53b commit d61edf4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/userprog/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static struct list all_list;
static thread_func start_process NO_RETURN;
static bool load(const char *cmdline, void (**eip)(void), void **esp);
static void process_load_fail(void);
static void process_load_success(void);
static void process_load_success(const char *cmd);

typedef void (*ret_addr_t)(void);
typedef union
Expand Down Expand Up @@ -111,16 +111,16 @@ static void start_process(void *file_name_)
char *cmd = strtok_r(file_name, " ", &save_ptr);
success = load(cmd, &if_.eip, &if_.esp);

/* If load successed, pass arguments. */
if (success)
{
if_.esp = arg_pass((esp_t)if_.esp, cmd, save_ptr);
process_load_success(cmd);
}

/* Free file_name whether successed or failed. */
palloc_free_page(file_name);

if (success)
process_load_success();
else
if (!success)
process_load_fail();

/* Start the user process by simulating a return from an
Expand Down Expand Up @@ -227,6 +227,11 @@ void process_exit(void)
struct process *self = cur->process;
self->thread = NULL;
self->status = PROCESS_EXITED;

ASSERT(self->file != NULL);
file_allow_write(self->file);
file_close(self->file);

sema_up(&self->sema_wait);
}

Expand Down Expand Up @@ -634,11 +639,13 @@ static void process_load_fail(void)
}

/* Set process status when load successed. */
static void process_load_success(void)
static void process_load_success(const char *cmd)
{
struct process *self = thread_current()->process;

self->status = PROCESS_NORMAL;
self->file = filesys_open(cmd);
file_deny_write(self->file);

sema_up(&self->sema_load);
}
1 change: 1 addition & 0 deletions src/userprog/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct process
struct semaphore sema_wait; /* Parent block on this while waiting. */
struct list files; /* Opening files. */
int fd; /* Max file descriptor num. */
struct file *file; /* Executable file loaded by self. */
};

void process_init(void);
Expand Down

0 comments on commit d61edf4

Please sign in to comment.