Skip to content

Commit c9e3c08

Browse files
committed
validate env limits in qfile-dom0-unpacker
Signed-off-by: Rishi Jat <rishijat098@gmail.com>
1 parent 455d4e7 commit c9e3c08

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

dom0-updates/qfile-dom0-unpacker.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,32 @@
1919
#define min(a,b) ((a) < (b) ? (a) : (b))
2020
#define max(a,b) ((a) > (b) ? (a) : (b))
2121

22+
static long long parse_limit_env(const char *name, long long fallback)
23+
{
24+
const char *value = getenv(name);
25+
if (!value)
26+
return fallback;
27+
28+
if (*value < '0' || *value > '9') {
29+
fprintf(stderr, "Invalid value for %s: %s\n", name, value);
30+
exit(1);
31+
}
32+
33+
errno = 0;
34+
char *end = NULL;
35+
long long limit = strtoll(value, &end, 10);
36+
if (errno == ERANGE || *end != '\0' || limit < 0) {
37+
fprintf(stderr, "Invalid value for %s: %s\n", name, value);
38+
exit(1);
39+
}
40+
if (limit == 0 && strcmp(value, "0") != 0) {
41+
fprintf(stderr, "Invalid value for %s: %s\n", name, value);
42+
exit(1);
43+
}
44+
45+
return limit;
46+
}
47+
2248
int prepare_creds_return_uid(const char *username)
2349
{
2450
struct passwd *pwd;
@@ -88,10 +114,8 @@ int main(int argc, char ** argv)
88114
perror("Failed to check free space");
89115
}
90116

91-
if ((var=getenv("UPDATES_MAX_BYTES")))
92-
bytes_limit = atoll(var);
93-
if ((var=getenv("UPDATES_MAX_FILES")))
94-
files_limit = atoll(var);
117+
bytes_limit = parse_limit_env("UPDATES_MAX_BYTES", bytes_limit);
118+
files_limit = parse_limit_env("UPDATES_MAX_FILES", files_limit);
95119

96120
set_size_limit(bytes_limit, files_limit);
97121

0 commit comments

Comments
 (0)