From 3bd73d07a90d1fc48f72df8505451ed0a3b3572e Mon Sep 17 00:00:00 2001 From: Thomas Hobson Date: Wed, 28 Apr 2021 16:03:35 +1200 Subject: [PATCH] file persistance fix --- api/src/globals.js | 8 +++++++- api/src/job.js | 32 +++++++++++++++++++++++++++----- tests/file_persistance.py | 25 +++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 tests/file_persistance.py diff --git a/api/src/globals.js b/api/src/globals.js index 300558e..e632a88 100644 --- a/api/src/globals.js +++ b/api/src/globals.js @@ -16,5 +16,11 @@ module.exports = { }, version: require('../package.json').version, platform, - pkg_installed_file: '.ppman-installed' //Used as indication for if a package was installed + pkg_installed_file: '.ppman-installed', //Used as indication for if a package was installed + clean_directories: [ + "/dev/shm", + "/run/lock", + "/tmp", + "/var/tmp" + ] }; diff --git a/api/src/job.js b/api/src/job.js index 6858c3a..d35893a 100644 --- a/api/src/job.js +++ b/api/src/job.js @@ -183,12 +183,10 @@ class Job { }; } - async cleanup() { - logger.info(`Cleaning up job uuid=${this.uuid}`); - await fs.rm(this.dir, { recursive: true, force: true }); + + async cleanup_processes(){ let processes = [1]; while(processes.length > 0){ - processes = await ps_list(); processes = processes.filter(proc => proc.uid == this.uid); @@ -212,8 +210,32 @@ class Job { wait_pid(proc.pid); } } - + } + async cleanup_filesystem(){ + /* + for (const clean_path of globals.clean_directories) { + const contents = await fs.readdir(clean_path); + + for (const file of contents) { + const file_path = path.join(clean_path, file); + const stat = await fs.stat(file_path); + if(stat.uid == this.uid) + await fs.rm(file_path, { recursive: true, force: true }); + } + + }*/ + + await fs.rm(this.dir, { recursive: true, force: true }); + } + + async cleanup() { + logger.info(`Cleaning up job uuid=${this.uuid}`); + + await Promise.all([ + this.cleanup_processes(), + this.cleanup_filesystem() + ]); } } diff --git a/tests/file_persistance.py b/tests/file_persistance.py new file mode 100644 index 0000000..ec23cfe --- /dev/null +++ b/tests/file_persistance.py @@ -0,0 +1,25 @@ +""" +Description + Files can be written into world writable directories without being removed, + potentially leading to disk space exhaustion + + Run this test twice and there should be no output + +""" + +import os + +directories = [ + "/dev/shm", + "/run/lock", + "/tmp", + "/var/tmp" +] + +for dir in directories: + fpath = f"{dir}/bean" + if os.path.exists(fpath): + print(f"{fpath} exists") + else: + with open(fpath, "w") as f: + f.write("beannn") \ No newline at end of file