diff --git a/remux.c b/remux.c index 9c344cbab0c9e751d6a9aaa5385284cd657471bc..4b2950f7de04efaa6cfee2c884d1d7b8762170a8 100644 --- a/remux.c +++ b/remux.c @@ -23,6 +23,7 @@ int main(int argc, char *argv[]) jobid = atoi(argv[1]); path = mprintf("%s/%s", getenv(WORKER_RELEASED), jstr(jlookup(argv[4], "path"), "")); tmp = mprintf("%s/.tmp-%i", getenv(WORKER_TMP), jobid); + overwrite_check(path); ping_job(jobid, "running", 0); demux = 0; @@ -74,6 +75,9 @@ int main(int argc, char *argv[]) if (err = av_write_trailer(mux)) job_failed("Error writing trailer to temporary file", av_err2str(err)); avio_closep(&mux->pb); + if (!filesize(tmp)) + job_failed("Sanity check failed: Output file is empty"); + overwrite_check(path); if (rename(tmp, path)) job_failed("Overwriting output file failed: %s", strerror(errno)); unlink(tmp); diff --git a/thumbnail.c b/thumbnail.c index a523c637056fc13c35e9e450fcbddeb826f620e1..bfc0ebcb912e7541eec17dd2f397022d20f2a349 100644 --- a/thumbnail.c +++ b/thumbnail.c @@ -98,6 +98,8 @@ int main(int argc, char *argv[]) if (err = av_write_trailer(mux)) job_failed("Error writing trailer to temporary file: %s", av_err2str(err)); avio_closep(&mux->pb); + if (!filesize(tmp)) + job_failed("Sanity check failed: Output file is empty"); if (rename(tmp, dest)) job_failed("Overwriting output file failed: %s", strerror(errno)); unlink(tmp); diff --git a/transcode.c b/transcode.c index 73c999b44439b645d43e0d3cdc25a735bb08f308..175f913ce2409e0159645891a05e435da30163bc 100644 --- a/transcode.c +++ b/transcode.c @@ -274,6 +274,7 @@ int main(int argc, char *argv[]) output = jlookup(argv[4], "output"); outpath = mprintf("%s/%s", getenv(WORKER_RELEASED), jstr(jlookup(output, "path"), "")); tmppath = mprintf("%s/.tmp-%i", getenv(WORKER_TMP), jobid); + overwrite_check(outpath); demux = 0; opts = 0; @@ -356,6 +357,9 @@ int main(int argc, char *argv[]) if (err = av_write_trailer(mux)) job_failed("Error writing trailer to temporary file", av_err2str(err)); avio_closep(&mux->pb); + if (!filesize(tmppath)) + job_failed("Sanity check failed: Output file is empty"); + overwrite_check(outpath); if (rename(tmppath, outpath)) job_failed("Overwriting output file \"%s\" failed: %s", outpath, strerror(errno)); unlink(tmppath); diff --git a/util.h b/util.h index a8585f113b9fd150a4b05f693c77bbd7cd182b4a..ce56e12dafebc2fabea70ae8886747374ca901c7 100644 --- a/util.h +++ b/util.h @@ -16,6 +16,7 @@ char *hashfile(char *path); double fileduration(char *path); size_t filesize(char *path); char *json_fileinfo(char *path); +void overwrite_check(char *path); #define WORKER_APIKEY "WORKER_APIKEY" #define WORKER_APIBASE "WORKER_APIBASE" diff --git a/util/overwrite_check.c b/util/overwrite_check.c new file mode 100644 index 0000000000000000000000000000000000000000..9dcbf9fb0ceaf9f019df7ce8d827df66a6f292e2 --- /dev/null +++ b/util/overwrite_check.c @@ -0,0 +1,14 @@ +#include <unistd.h> +#include <sys/stat.h> + +#include "../util.h" + +void overwrite_check(char *path) +{ + struct stat s; + if (stat(path, &s) || !s.st_size) + return; /* We can overwrite non-existing or empty files */ + if (s.st_uid != getuid()) + job_failed("Refusing to overwrite output file \"%s\": File was not created by worker"); +} +