Thread View: linux.debian.bugs.dist
1 messages
1 total messages
Started by Georges Khaznadar
Tue, 03 Nov 2026 17:10
Bug#1152897: cron: environment-heavy crontabs can stall the daemon during database reload
Author: Georges Khaznadar
Date: Tue, 03 Nov 2026 17:10
Date: Tue, 03 Nov 2026 17:10
1145 lines
38289 bytes
38289 bytes
--OjoGCRX0Gaxw+7po
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Hello,
I wonder why crontab installers should care for misbehavior of
users. I agree that one user can overload a computer with multiple
environment assignments, triggered by a crontab.
By the way, do **you** create such crontabs? Why would a sane user
create such crontabs?
If some user **wants* to overload a system, they only need to launch
multiple resource-consuming tasks. There is no need to build a crontab
for that purpose.
Dear Brett, please provide some evidence that a cron package with your
enhancement would protect efficiently a computer against a user who
wants to overload the machine.
Best regards, Georges.
Brett Sutton a =C3=A9crit :
> Package: cron
> Version: 3.0pl1-212
> Severity: important
> Tags: patch
>=20
> An account permitted to install a crontab can substantially delay the
> central cron daemon by supplying many environment assignments.
> Debian's crontab command validates these assignments cheaply, while
> the daemon repeatedly scans and reallocates its growing environment
> during load_user(). With many distinct names sharing long prefixes,
> loading grows approximately quadratically.
>=20
> Isolated measurements on the packaged code: 1,000 assignments parsed
> in 0.23 s; 8,000 in 14.35 s. A 9.28 MB crontab was accepted by
> crontab(1) in about 0.2 s, then took about 22 s in the daemon's
> database-loading path. Changing and reloading it incurred a similar
> delay. We have not established the maximum practical delay or
> demonstrated an out-of-memory failure. The daemon loads synchronously,
> so other jobs can be delayed during the parse.
>=20
> The attached Debian -212 quilt patch replaces repeated linear
> environment-name lookup with a balanced index and geometrically grown
> ordered vector, retaining last-value-wins and assignment order. It
> also adds a 16 MiB whole-crontab size limit and a 16 MiB accounting
> budget for copied job environments. Comments count toward file size
> but are skipped by environment parsing. The budget is conservative for
> repeated replacements. Both limits are checked at installation and
> load time, with recovery when an oversized system file is corrected.
>=20
> The patched Debian source built in a disposable trixie container. All
> 13 existing Debian integration tests passed, as did focused
> environment, allocation-failure, installer-budget and loader-recovery
> tests. The patch includes regression sources; build/test integration
> for the complete patch series is available on request.
>=20
> This affects Ubuntu cron 3.0pl1-200ubuntu1 and -209ubuntu1 as well.
> Debian and Ubuntu Security Teams reviewed the findings and recommended
> public bug reports. No privilege escalation was demonstrated.
>=20
> Regards,
> Brett Sutton
> Description: Bound crontab resource use and index environment assignments
> Origin: other, private Daybreak audit
> Forwarded: no
>=20
> --- a/Makefile
> +++ b/Makefile
> @@ -88,16 +88,16 @@
> =20
> INFOS =3D README CHANGES FEATURES INSTALL CONVERSION THANKS MAIL
> MANPAGES =3D bitstring.3 crontab.5 crontab.1 cron.8 putman.sh
> -HEADERS =3D bitstring.h cron.h config.h pathnames.h \
> +HEADERS =3D bitstring.h cron.h cron_limits.h env_builder.h config.h pat=
hnames.h \
> externs.h compat.h
> SOURCES =3D cron.c crontab.c database.c do_command.c entry.c \
> - env.c job.c user.c popen.c misc.c compat.c
> + env.c env_builder.c job.c user.c popen.c misc.c compat.c
> SHAR_SOURCE =3D $(INFOS) $(MANPAGES) Makefile $(HEADERS) $(SOURCES)
> LINT_CRON =3D cron.c database.c user.c entry.c compat.c \
> - misc.c job.c do_command.c env.c popen.c
> + misc.c job.c do_command.c env.c env_builder.c popen.c
> LINT_CRONTAB =3D crontab.c misc.c entry.c env.c compat.c
> CRON_OBJ =3D cron.o database.o user.o entry.o job.o do_command.o \
> - misc.o env.o popen.o compat.o
> + misc.o env.o env_builder.o popen.o compat.o
> CRONTAB_OBJ =3D crontab.o misc.o entry.o env.o compat.o
> =20
> all : cron crontab
> @@ -127,5 +127,5 @@
> kit : $(SHAR_SOURCE)
> makekit -m -s99k $(SHAR_SOURCE)
> =20
> -$(CRON_OBJ) : cron.h compat.h config.h externs.h pathnames.h bitstring.h=
Makefile
> -$(CRONTAB_OBJ) : cron.h compat.h config.h externs.h pathnames.h bitstrin=
g.h Makefile
> +$(CRON_OBJ) : cron.h cron_limits.h env_builder.h compat.h config.h exter=
ns.h pathnames.h bitstring.h Makefile
> +$(CRONTAB_OBJ) : cron.h cron_limits.h env_builder.h compat.h config.h ex=
terns.h pathnames.h bitstring.h Makefile
> --- a/cron.h
> +++ b/cron.h
> @@ -83,6 +83,9 @@
> #define MAX_TEMPSTR 1000 /* max length of envvar=3Dvalue\0 strings */
> #define MAX_ENVSTR MAX_TEMPSTR /* DO NOT change - buffer overruns otherw=
ise */
> #define MAX_TAB_LINES 10000 /* max length of crontabs */
> +/* Bounds apply to both installation and daemon loading. */
> +#define MAX_CRONTAB_BYTES (16 * 1024 * 1024)
> +#define MAX_ENV_SNAPSHOTS_BYTES (16 * 1024 * 1024)
> #define MAX_UNAME 20 /* max length of username, should be overkill */
> #define ROOT_UID 0 /* don't change this, it really must be root */
> #define ROOT_USER "root" /* ditto */
> --- /dev/null
> +++ b/cron_limits.h
> @@ -0,0 +1,70 @@
> +#ifndef CRON_LIMITS_H
> +#define CRON_LIMITS_H
> +
> +#include <stddef.h>
> +#include <string.h>
> +#include <stdint.h>
> +#include <pwd.h>
> +
> +struct cron_limits {
> + size_t assignments;
> + size_t env_bytes;
> + size_t snapshots_bytes;
> +};
> +
> +static inline int
> +cron_account_env(struct cron_limits *limits, const char *assignment)
> +{
> + size_t bytes =3D strlen(assignment) + 1;
> + if (limits->assignments =3D=3D SIZE_MAX ||
> + bytes > SIZE_MAX - limits->env_bytes)
> + return 0;
> + limits->assignments++;
> + limits->env_bytes +=3D bytes;
> + return 1;
> +}
> +
> +static inline int
> +cron_take_budget(size_t *remaining, size_t bytes)
> +{
> + if (bytes > *remaining)
> + return 0;
> + *remaining -=3D bytes;
> + return 1;
> +}
> +
> +static inline size_t
> +cron_default_assignment_bytes(const char *prefix, const char *value)
> +{
> + size_t prefix_bytes =3D strlen(prefix);
> + size_t value_bytes =3D strlen(value);
> + if (value_bytes >=3D MAX_ENVSTR - prefix_bytes)
> + return MAX_ENVSTR;
> + return prefix_bytes + value_bytes + 1;
> +}
> +
> +static inline int
> +cron_account_entry(struct cron_limits *limits, const struct passwd *pw)
> +{
> + /* Overcount replacements and defaults to bound per-job environment cop=
ies. */
> + size_t remaining =3D MAX_ENV_SNAPSHOTS_BYTES - limits->snapshots_bytes;
> + size_t defaults =3D cron_default_assignment_bytes("SHELL=3D", _PATH_BSH=
ELL) +
> + (pw ? cron_default_assignment_bytes("HOME=3D", pw->pw_dir) : MAX_ENVST=
R) +
> + cron_default_assignment_bytes("PATH=3D", _PATH_DEFPATH) +
> + (pw ? cron_default_assignment_bytes("LOGNAME=3D", pw->pw_name) : MAX_E=
NVSTR);
> +#if defined(BSD)
> + defaults +=3D pw ? cron_default_assignment_bytes("USER=3D", pw->pw_name=
) : MAX_ENVSTR;
> +#endif
> + if (limits->assignments > (SIZE_MAX / 64) - 6)
> + return 0;
> + if (!cron_take_budget(&remaining, limits->env_bytes) ||
> + !cron_take_budget(&remaining, defaults) ||
> + !cron_take_budget(&remaining,
> + (limits->assignments + 6) * sizeof(char *)) ||
> + !cron_take_budget(&remaining, (limits->assignments + 5) * 64))
> + return 0;
> + limits->snapshots_bytes =3D MAX_ENV_SNAPSHOTS_BYTES - remaining;
> + return 1;
> +}
> +
> +#endif
> --- a/crontab.5
> +++ b/crontab.5
> @@ -329,6 +329,16 @@
> All of the `@' commands that can appear in place of the first five fields
> are extensions.
> .SH LIMITATIONS
> +A crontab is limited to 16 MiB including comments and its generated head=
er.
> +The accumulated environments copied into job entries have a 16 MiB budge=
t.
> +These limits are checked when a user installs a crontab and when the dae=
mon
> +loads it. A preexisting file that exceeds a limit is ignored by the daem=
on
> +until it is reduced. The environment-copy budget is an accounting estima=
te,
> +not a process memory limit. It conservatively counts all preceding envir=
onment
> +assignments, including replaced values, and reserves space for default v=
alues.
> +Consequently, repeated replacements can exhaust this budget even when th=
e live
> +environment is small.
> +.PP
> The
> .I cron
> daemon runs with a defined timezone. It currently does not support
> --- a/crontab.c
> +++ b/crontab.c
> @@ -29,6 +29,7 @@
> =20
> =20
> #include "cron.h"
> +#include "cron_limits.h"
> #include "misc.h"
> =20
> #include <errno.h>
> @@ -829,6 +830,8 @@
> char n[MAX_FNAME], envstr[MAX_ENVSTR];
> FILE *tmp;
> int ch, eof, fd;
> + size_t input_bytes =3D 0;
> + struct cron_limits limits =3D {0};
> int nl =3D FALSE;
> entry *e;
> time_t now =3D time(NULL);
> @@ -875,9 +878,16 @@
> /* copy the crontab to the tmp
> */
> rewind(NewCrontab);
> + input_bytes =3D (size_t)ftell(tmp);
> Set_LineNum(1)
> - while (EOF !=3D (ch =3D get_char(NewCrontab)))
> + while (EOF !=3D (ch =3D get_char(NewCrontab))) {
> + if (++input_bytes > MAX_CRONTAB_BYTES) {
> + fprintf(stderr, "crontab exceeds %d bytes.\n", MAX_CRONTAB_BYTES);
> + fclose(tmp); unlink(tn);
> + return (-1);
> + }
> putc(ch, tmp);
> + }
> =20
> if (ferror(tmp) || fflush(tmp) || fsync(fd)) {
> fprintf(stderr, "%s: %s: %s\n",
> @@ -903,11 +913,19 @@
> nl =3D TRUE;
> break;
> case FALSE:
> + if (!cron_account_entry(&limits, pw)) {
> + check_error("job environments exceed memory budget");
> + break;
> + }
> e =3D load_entry(tmp, check_error, pw, envp);
> if (e)
> free(e);
> break;
> case TRUE:
> + if (!cron_account_env(&limits, envstr)) {
> + check_error("environment exceeds memory budget");
> + break;
> + }
> /* here MAILTO and MAILFROM are checked */
> if (
> strncmp(envstr, "MAILTO=3D", 7) =3D=3D 0 ||
> --- a/database.c
> +++ b/database.c
> @@ -436,6 +436,12 @@
> */
> Debug(DLOAD, ("\t%s:", fname))
> =20
> + if (statbuf->st_size > MAX_CRONTAB_BYTES) {
> + log_it(fname, getpid(), "CRONTAB TOO LARGE", tabname);
> + force_rescan_user(old_db, new_db, fname, statbuf->st_mtime);
> + goto next_crontab;
> + }
> +
> if (old_db !=3D NULL)
> u =3D find_user(old_db, fname);
> =20
> --- a/env.c
> +++ b/env.c
> @@ -79,6 +79,7 @@
> {
> register int count, found;
> register char **p;
> + char *new_value;
> =20
> /*
> * count the number of elements, including the null pointer;
> @@ -96,12 +97,12 @@
> * it exists already, so just free the existing setting,
> * save our new one there, and return the existing array.
> */
> - free(envp[found]);
> - if ((envp[found] =3D strdup(envstr)) =3D=3D NULL) {
> - envp[found] =3D "";
> + if ((new_value =3D strdup(envstr)) =3D=3D NULL) {
> errno =3D ENOMEM;
> return NULL;
> }
> + free(envp[found]);
> + envp[found] =3D new_value;
> return (envp);
> }
> =20
> @@ -110,17 +111,19 @@
> * one, save our string over the old null pointer, and return resized
> * array.
> */
> + if ((new_value =3D strdup(envstr)) =3D=3D NULL) {
> + errno =3D ENOMEM;
> + return NULL;
> + }
> p =3D (char **) realloc((void *) envp,
> (unsigned) ((count+1) * sizeof(char **)));
> if (p =3D=3D NULL) {
> + free(new_value);
> errno =3D ENOMEM;
> return NULL;
> }
> p[count] =3D p[count-1];
> - if ((p[count-1] =3D strdup(envstr)) =3D=3D NULL) {
> - errno =3D ENOMEM;
> - return NULL;
> - }
> + p[count-1] =3D new_value;
> return (p);
> }
> =20
> --- /dev/null
> +++ b/env_builder.c
> @@ -0,0 +1,100 @@
> +#include "cron.h"
> +#include "env_builder.h"
> +
> +#include <search.h>
> +#include <string.h>
> +
> +struct env_key {
> + char *assignment;
> + size_t index;
> +};
> +
> +static int
> +compare_env_keys(const void *left, const void *right)
> +{
> + const char *a =3D ((const struct env_key *)left)->assignment;
> + const char *b =3D ((const struct env_key *)right)->assignment;
> + unsigned char ac, bc;
> +
> + for (;;) {
> + ac =3D *a =3D=3D '=3D' ? 0 : (unsigned char)*a;
> + bc =3D *b =3D=3D '=3D' ? 0 : (unsigned char)*b;
> + if (ac !=3D bc || ac =3D=3D 0)
> + return (int)ac - (int)bc;
> + a++;
> + b++;
> + }
> +}
> +
> +int
> +env_builder_init(struct env_builder *builder)
> +{
> + memset(builder, 0, sizeof *builder);
> + builder->values =3D env_init();
> + return builder->values !=3D NULL;
> +}
> +
> +int
> +env_builder_set(struct env_builder *builder, const char *assignment)
> +{
> + struct env_key query =3D {(char *)assignment, 0};
> + struct env_key *key;
> + void *node;
> + char *copy;
> + char **values;
> + size_t capacity;
> +
> + node =3D tfind(&query, &builder->index, compare_env_keys);
> + if ((copy =3D strdup(assignment)) =3D=3D NULL)
> + return 0;
> + if (node !=3D NULL) {
> + key =3D *(struct env_key **)node;
> + free(builder->values[key->index]);
> + builder->values[key->index] =3D copy;
> + key->assignment =3D copy;
> + return 1;
> + }
> +
> + key =3D malloc(sizeof *key);
> + if (key =3D=3D NULL) {
> + free(copy);
> + return 0;
> + }
> + if (builder->count =3D=3D builder->capacity) {
> + capacity =3D builder->capacity ? builder->capacity * 2 : 8;
> + values =3D realloc(builder->values, (capacity + 1) * sizeof *values);
> + if (values =3D=3D NULL) {
> + free(key);
> + free(copy);
> + return 0;
> + }
> + builder->values =3D values;
> + builder->capacity =3D capacity;
> + }
> + key->assignment =3D copy;
> + key->index =3D builder->count;
> + node =3D tsearch(key, &builder->index, compare_env_keys);
> + if (node =3D=3D NULL) {
> + free(key);
> + free(copy);
> + return 0;
> + }
> + builder->values[builder->count++] =3D copy;
> + builder->values[builder->count] =3D NULL;
> + return 1;
> +}
> +
> +void
> +env_builder_destroy(struct env_builder *builder)
> +{
> + size_t i;
> + for (i =3D 0; i < builder->count; i++) {
> + struct env_key query =3D {builder->values[i], 0};
> + void *node =3D tfind(&query, &builder->index, compare_env_keys);
> + if (node !=3D NULL) {
> + struct env_key *key =3D *(struct env_key **)node;
> + tdelete(key, &builder->index, compare_env_keys);
> + free(key);
> + }
> + }
> +}
> --- /dev/null
> +++ b/env_builder.h
> @@ -0,0 +1,18 @@
> +#ifndef CRON_ENV_BUILDER_H
> +#define CRON_ENV_BUILDER_H
> +
> +#include <stddef.h>
> +
> +/* Build the ordered, NULL-terminated environment passed to load_entry()=
=2E */
> +struct env_builder {
> + char **values;
> + size_t count;
> + size_t capacity;
> + void *index;
> +};
> +
> +int env_builder_init(struct env_builder *builder);
> +int env_builder_set(struct env_builder *builder, const char *assignment);
> +void env_builder_destroy(struct env_builder *builder);
> +
> +#endif
> --- a/user.c
> +++ b/user.c
> @@ -26,6 +26,8 @@
> #include <syslog.h>
> #include <string.h>
> #include "cron.h"
> +#include "cron_limits.h"
> +#include "env_builder.h"
> =20
> =20
> #ifdef WITH_SELINUX
> @@ -205,7 +207,9 @@
> user *u;
> entry *e;
> int status;
> - char **envp =3D NULL, **tenvp;
> + struct cron_limits limits =3D {0};
> + char **envp;
> + struct env_builder builder =3D {0};
> =20
> if (!(file =3D fdopen(crontab_fd, "r"))) {
> perror("fdopen on crontab_fd in load_user");
> @@ -247,11 +251,12 @@
> /*=20
> * init environment. this will be copied/augmented for each entry.
> */
> - if ((envp =3D env_init()) =3D=3D NULL) {
> + if (!env_builder_init(&builder)) {
> free(u->name);
> free(u);
> return NULL;
> }
> + envp =3D builder.values;
> =20
> /*
> * load the crontab
> @@ -276,6 +281,12 @@
> }
> goto done;
> case FALSE:
> + if (!cron_account_entry(&limits, pw)) {
> + log_it(u->name, getpid(), "ERROR", "job environments exceed memory b=
udget");
> + free_user(u);
> + u =3D NULL;
> + goto done;
> + }
> err_user =3D fname;
> e =3D load_entry(file, crontab_error, pw, envp);
> err_user =3D NULL;
> @@ -293,8 +304,14 @@
> }
> break;
> case TRUE:
> - if ((tenvp =3D env_set(envp, envstr))) {
> - envp =3D tenvp;
> + if (!cron_account_env(&limits, envstr)) {
> + log_it(u->name, getpid(), "ERROR", "environment exceeds memory budge=
t");
> + free_user(u);
> + u =3D NULL;
> + goto done;
> + }
> + if (env_builder_set(&builder, envstr)) {
> + envp =3D builder.values;
> } else {
> free_user(u);
> u =3D NULL;
> @@ -317,7 +334,8 @@
> }
> =20
> done:
> - env_free(envp);
> + env_builder_destroy(&builder);
> + env_free(builder.values);
> fclose(file);
> Debug(DPARS, ("...load_user() done\n"))
> return u;
> --- /dev/null
> +++ b/tests/env_builder_failure.c
> @@ -0,0 +1,84 @@
> +#include "../cron.h"
> +#include "../env_builder.h"
> +
> +#include <assert.h>
> +#include <search.h>
> +#include <string.h>
> +
> +enum failure { NONE, FAIL_STRDUP, FAIL_MALLOC, FAIL_REALLOC, FAIL_TSEARC=
H };
> +static enum failure fail_next;
> +
> +void *__real_malloc(size_t);
> +void *__real_realloc(void *, size_t);
> +char *__real_strdup(const char *);
> +void *__real_tsearch(const void *, void **, int (*)(const void *, const =
void *));
> +
> +void *__wrap_malloc(size_t size)
> +{
> + if (fail_next =3D=3D FAIL_MALLOC) {
> + fail_next =3D NONE;
> + return NULL;
> + }
> + return __real_malloc(size);
> +}
> +
> +void *__wrap_realloc(void *ptr, size_t size)
> +{
> + if (fail_next =3D=3D FAIL_REALLOC) {
> + fail_next =3D NONE;
> + return NULL;
> + }
> + return __real_realloc(ptr, size);
> +}
> +
> +char *__wrap_strdup(const char *value)
> +{
> + if (fail_next =3D=3D FAIL_STRDUP) {
> + fail_next =3D NONE;
> + return NULL;
> + }
> + return __real_strdup(value);
> +}
> +
> +void *__wrap_tsearch(const void *key, void **root,
> + int (*compare)(const void *, const void *))
> +{
> + if (fail_next =3D=3D FAIL_TSEARCH) {
> + fail_next =3D NONE;
> + return NULL;
> + }
> + return __real_tsearch(key, root, compare);
> +}
> +
> +int main(void)
> +{
> + struct env_builder builder;
> + char assignment[32];
> + int i;
> + assert(env_builder_init(&builder));
> + assert(env_builder_set(&builder, "A=3Dold"));
> + fail_next =3D FAIL_STRDUP;
> + assert(!env_builder_set(&builder, "A=3Dnew"));
> + assert(strcmp(builder.values[0], "A=3Dold") =3D=3D 0);
> + fail_next =3D FAIL_MALLOC;
> + assert(!env_builder_set(&builder, "B=3D1"));
> + assert(builder.count =3D=3D 1 && builder.values[1] =3D=3D NULL);
> + fail_next =3D FAIL_TSEARCH;
> + assert(!env_builder_set(&builder, "B=3D1"));
> + assert(builder.count =3D=3D 1 && builder.values[1] =3D=3D NULL);
> + for (i =3D 1; i < 8; i++) {
> + snprintf(assignment, sizeof assignment, "K%d=3Dx", i);
> + assert(env_builder_set(&builder, assignment));
> + }
> + assert(builder.count =3D=3D 8 && builder.capacity =3D=3D 8);
> + fail_next =3D FAIL_REALLOC;
> + assert(!env_builder_set(&builder, "K8=3Dx"));
> + assert(builder.count =3D=3D 8 && builder.values[8] =3D=3D NULL);
> + assert(builder.capacity =3D=3D 8);
> + assert(env_builder_set(&builder, "K8=3Dx"));
> + assert(builder.count =3D=3D 9 && builder.capacity =3D=3D 16);
> + env_builder_destroy(&builder);
> + env_free(builder.values);
> + puts("environment builder failure tests passed");
> + return 0;
> +}
> --- /dev/null
> +++ b/tests/installer_limits.c
> @@ -0,0 +1,111 @@
> +#define MAIN_PROGRAM
> +#include "../cron.h"
> +
> +#include <assert.h>
> +#include <fcntl.h>
> +#include <sys/stat.h>
> +#include <unistd.h>
> +#include "budget_fixture.h"
> +
> +static int
> +install_path(const char *path, int dry_run)
> +{
> + int status;
> + pid_t child =3D fork();
> + assert(child >=3D 0);
> + if (child =3D=3D 0) {
> + if (dry_run)
> + execl("/usr/bin/crontab", "crontab", "-n", path, (char *)NULL);
> + else
> + execl("/usr/bin/crontab", "crontab", path, (char *)NULL);
> + _exit(127);
> + }
> + assert(waitpid(child, &status, 0) =3D=3D child);
> + assert(WIFEXITED(status));
> + return WEXITSTATUS(status);
> +}
> +
> +static void
> +check_installed_jobs(int expected)
> +{
> + FILE *file =3D popen("/usr/bin/crontab -l", "r");
> + char line[2048];
> + int count =3D 0;
> + assert(file !=3D NULL);
> + while (fgets(line, sizeof line, file))
> + if (strcmp(line, "* * * * * true\n") =3D=3D 0)
> + count++;
> + assert(pclose(file) =3D=3D 0);
> + assert(count =3D=3D expected);
> +}
> +
> +static void
> +check_environment_budget(void)
> +{
> + char path[] =3D "/tmp/daybreak-env.XXXXXX";
> + int fd =3D mkstemp(path);
> + FILE *file;
> + assert(fd >=3D 0);
> + file =3D fdopen(fd, "w");
> + assert(file !=3D NULL);
> + write_budget_fixture(file, 0, 50);
> + assert(fclose(file) =3D=3D 0);
> + assert(install_path(path, 1) =3D=3D 0);
> + assert(install_path(path, 0) =3D=3D 0);
> + check_installed_jobs(51);
> + file =3D fopen(path, "w");
> + assert(file !=3D NULL);
> + write_budget_fixture(file, 0, 90);
> + assert(fclose(file) =3D=3D 0);
> + assert(install_path(path, 1) !=3D 0);
> + assert(install_path(path, 0) !=3D 0);
> + /* Failed replacement must retain the previously installed full table. =
*/
> + check_installed_jobs(51);
> + file =3D fopen(path, "w");
> + assert(file !=3D NULL);
> + fputs("* * * * * true\n", file);
> + assert(fclose(file) =3D=3D 0);
> + assert(install_path(path, 0) =3D=3D 0);
> + check_installed_jobs(1);
> + assert(unlink(path) =3D=3D 0);
> +}
> +
> +static int
> +check_crontab_size(off_t bytes)
> +{
> + char path[] =3D "/tmp/daybreak-crontab.XXXXXX";
> + int fd =3D mkstemp(path), status;
> + pid_t child;
> + assert(fd >=3D 0 && bytes >=3D 2);
> + assert(write(fd, "#", 1) =3D=3D 1);
> + assert(ftruncate(fd, bytes) =3D=3D 0);
> + assert(lseek(fd, bytes - 1, SEEK_SET) =3D=3D bytes - 1);
> + assert(write(fd, "\n", 1) =3D=3D 1);
> + close(fd);
> + child =3D fork();
> + assert(child >=3D 0);
> + if (child =3D=3D 0) {
> + int null_fd =3D open("/dev/null", O_WRONLY);
> + assert(null_fd >=3D 0);
> + dup2(null_fd, STDERR);
> + close(null_fd);
> + execl("/usr/bin/crontab", "crontab", "-n", path, (char *)NULL);
> + _exit(127);
> + }
> + assert(waitpid(child, &status, 0) =3D=3D child);
> + assert(unlink(path) =3D=3D 0);
> + assert(WIFEXITED(status));
> + return WEXITSTATUS(status);
> +}
> +
> +int
> +main(void)
> +{
> + assert(getenv("DAYBREAK_DISPOSABLE_TESTBED") !=3D NULL);
> + assert(geteuid() =3D=3D 0);
> + assert(check_crontab_size(MAX_CRONTAB_BYTES - 512) =3D=3D 0);
> + assert(check_crontab_size(MAX_CRONTAB_BYTES) !=3D 0);
> + check_environment_budget();
> + puts("installer file-size and environment budget tests passed");
> + return 0;
> +}
> --- /dev/null
> +++ b/tests/loader_recovery.c
> @@ -0,0 +1,87 @@
> +#define MAIN_PROGRAM
> +#include "../cron.h"
> +
> +#include <assert.h>
> +#include <fcntl.h>
> +#include <sys/stat.h>
> +#include <unistd.h>
> +#include "budget_fixture.h"
> +
> +/* Run only inside a disposable, root-owned package testbed. */
> +static const char path[] =3D SYSCRONDIR "/daybreak_recovery";
> +static const char name[] =3D "*system*daybreak_recovery";
> +
> +static void
> +write_test_file(off_t size, time_t mtime)
> +{
> + int fd =3D open(path, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0644);
> + struct timespec times[2] =3D {{mtime, 0}, {mtime, 0}};
> + static const char valid[] =3D "* * * * * root true\n";
> + if (fd < 0) {
> + fd =3D open(path, O_WRONLY | O_TRUNC);
> + assert(fd >=3D 0);
> + }
> + assert(fchmod(fd, 0644) =3D=3D 0);
> + if (size =3D=3D 0) {
> + assert(write(fd, valid, sizeof valid - 1) =3D=3D sizeof valid - 1);
> + } else {
> + assert(write(fd, "#", 1) =3D=3D 1);
> + assert(ftruncate(fd, size) =3D=3D 0);
> + }
> + assert(futimens(fd, times) =3D=3D 0);
> + close(fd);
> +}
> +
> +int
> +main(void)
> +{
> + cron_db db =3D {0};
> + user *loaded;
> + time_t now =3D time(NULL);
> + assert(getenv("DAYBREAK_DISPOSABLE_TESTBED") !=3D NULL);
> + assert(geteuid() =3D=3D 0);
> + assert(access(path, F_OK) !=3D 0);
> + assert(chdir(CRONDIR) =3D=3D 0);
> + write_test_file(0, now + 10);
> + load_database(&db);
> + loaded =3D find_user(&db, name);
> + assert(loaded !=3D NULL && loaded->crontab !=3D NULL);
> + write_test_file(MAX_CRONTAB_BYTES + 1, now + 20);
> + load_database(&db);
> + loaded =3D find_user(&db, name);
> + assert(loaded !=3D NULL && loaded->crontab =3D=3D NULL);
> + assert(loaded->mtime =3D=3D now + 20);
> + write_test_file(0, now + 30);
> + load_database(&db);
> + loaded =3D find_user(&db, name);
> + assert(loaded !=3D NULL && loaded->crontab !=3D NULL);
> + {
> + int jobs[] =3D {50, 90, 50};
> + size_t i;
> + for (i =3D 0; i < sizeof jobs / sizeof jobs[0]; i++) {
> + FILE *file =3D fopen(path, "w");
> + time_t stamp =3D now + 40 + i * 10;
> + struct timespec times[2] =3D {{stamp, 0}, {stamp, 0}};
> + assert(file !=3D NULL);
> + write_budget_fixture(file, 1, jobs[i]);
> + assert(fflush(file) =3D=3D 0);
> + assert(futimens(fileno(file), times) =3D=3D 0);
> + assert(fclose(file) =3D=3D 0);
> + load_database(&db);
> + loaded =3D find_user(&db, name);
> + assert(loaded !=3D NULL && loaded->mtime =3D=3D stamp);
> + if (jobs[i] =3D=3D 90) {
> + assert(loaded->crontab =3D=3D NULL);
> + } else {
> + entry *e;
> + int count =3D 0;
> + for (e =3D loaded->crontab; e; e =3D e->next)
> + count++;
> + assert(count =3D=3D jobs[i] + 1);
> + }
> + }
> + }
> + assert(unlink(path) =3D=3D 0);
> + puts("system crontab oversized-file recovery passed");
> + return 0;
> +}
> --- /dev/null
> +++ b/tests/selinux_cleanup.c
> @@ -0,0 +1,58 @@
> +#define MAIN_PROGRAM
> +#include "../cron.h"
> +
> +#include <assert.h>
> +#include <unistd.h>
> +
> +int __wrap_is_selinux_enabled(void) { return 1; }
> +int __wrap_getcon(security_context_t *context)
> +{
> + (void)context;
> + return -1;
> +}
> +
> +void log_it(char *username, int pid, char *event, char *detail)
> +{
> + (void)username;
> + (void)pid;
> + (void)event;
> + (void)detail;
> +}
> +
> +int __wrap_load_env(char *envstr, FILE *file)
> +{
> + (void)envstr;
> + (void)file;
> + return ERR;
> +}
> +
> +entry *load_entry(FILE *file, void (*error)(char *),
> + struct passwd *pw, char **envp)
> +{
> + (void)file;
> + (void)error;
> + (void)pw;
> + (void)envp;
> + return NULL;
> +}
> +
> +void free_entry(entry *item)
> +{
> + (void)item;
> +}
> +
> +int main(void)
> +{
> + FILE *file =3D tmpfile();
> + struct passwd pw =3D {0};
> + int fd;
> + assert(file !=3D NULL);
> + fd =3D dup(fileno(file));
> + assert(fd >=3D 0);
> + fclose(file);
> + pw.pw_name =3D "selinux-test";
> + pw.pw_dir =3D "/tmp";
> + assert(load_user(fd, &pw, "selinux-test", "selinux-test", "test-tab") =
=3D=3D NULL);
> + puts("SELinux early-cleanup test passed");
> + return 0;
> +}
> --- /dev/null
> +++ b/tests/resource_regression.c
> @@ -0,0 +1,89 @@
> +#define MAIN_PROGRAM
> +#include "../cron.h"
> +#include "../cron_limits.h"
> +#include "../env_builder.h"
> +#include <assert.h>
> +
> +static void
> +check_environment(void)
> +{
> + char **envp =3D env_init();
> + struct env_builder builder;
> + struct cron_limits limits =3D {0};
> + struct passwd account =3D {0};
> + char long_home[MAX_ENVSTR + 100];
> + char long_name[MAX_ENVSTR + 100];
> + char assignment[950];
> + int i;
> + static const char *settings[] =3D {
> + "A=3Dfirst", "B=3D", "AA=3Dlong prefix", "A=3Dlast", "B=3Dsecond",
> + "QUOTED=3Dhello world"
> + };
> + assert(envp !=3D NULL);
> + envp =3D env_set(envp, "A=3Dold");
> + assert(envp !=3D NULL);
> + envp =3D env_set(envp, "A=3Dnew");
> + assert(envp !=3D NULL && envp[1] =3D=3D NULL);
> + assert(strcmp(envp[0], "A=3Dnew") =3D=3D 0);
> + env_free(envp);
> + assert(env_builder_init(&builder));
> + envp =3D env_init();
> + assert(envp !=3D NULL);
> + for (i =3D 0; i < (int)(sizeof settings / sizeof settings[0]); i++) {
> + assert(env_builder_set(&builder, settings[i]));
> + envp =3D env_set(envp, (char *)settings[i]);
> + assert(envp !=3D NULL);
> + }
> + for (i =3D 0; envp[i] !=3D NULL; i++)
> + assert(strcmp(envp[i], builder.values[i]) =3D=3D 0);
> + assert(builder.values[i] =3D=3D NULL);
> + env_free(envp);
> + env_builder_destroy(&builder);
> + env_free(builder.values);
> + assert(env_builder_init(&builder));
> + assert(env_builder_set(&builder, "A=3Dold"));
> + memset(assignment, 'a', 900);
> + for (i =3D 0; i < 4000; i++) {
> + snprintf(assignment + 900, sizeof assignment - 900, "%05d=3Dx", i);
> + assert(cron_account_env(&limits, assignment));
> + assert(env_builder_set(&builder, assignment));
> + }
> + assert(env_builder_set(&builder, "A=3Dnew"));
> + assert(builder.count =3D=3D 4001);
> + assert(strcmp(builder.values[0], "A=3Dnew") =3D=3D 0);
> + assert(strstr(builder.values[4000], "03999=3Dx") !=3D NULL);
> + assert(builder.values[4001] =3D=3D NULL);
> + env_builder_destroy(&builder);
> + env_free(builder.values);
> + memset(long_home, 'h', sizeof long_home - 1);
> + memset(long_name, 'n', sizeof long_name - 1);
> + long_home[sizeof long_home - 1] =3D '\0';
> + long_name[sizeof long_name - 1] =3D '\0';
> + account.pw_dir =3D long_home;
> + account.pw_name =3D long_name;
> + assert(cron_account_entry(&limits, &account));
> + assert(limits.snapshots_bytes > limits.env_bytes + 2 * MAX_ENVSTR);
> + {
> + struct cron_limits exact =3D {0}, over =3D {0};
> + size_t entry_bytes =3D limits.snapshots_bytes;
> + exact.assignments =3D over.assignments =3D limits.assignments;
> + exact.env_bytes =3D over.env_bytes =3D limits.env_bytes;
> + exact.snapshots_bytes =3D MAX_ENV_SNAPSHOTS_BYTES - entry_bytes;
> + over.snapshots_bytes =3D exact.snapshots_bytes + 1;
> + assert(cron_account_entry(&exact, &account));
> + assert(exact.snapshots_bytes =3D=3D MAX_ENV_SNAPSHOTS_BYTES);
> + assert(!cron_account_entry(&over, &account));
> + assert(over.snapshots_bytes =3D=3D exact.snapshots_bytes - entry_bytes=
+ 1);
> + }
> + while (cron_account_entry(&limits, &account))
> + ;
> + assert(limits.snapshots_bytes <=3D MAX_ENV_SNAPSHOTS_BYTES);
> + limits =3D (struct cron_limits){0};
> + assert(cron_account_entry(&limits, NULL));
> + assert(limits.snapshots_bytes >=3D 2 * MAX_ENVSTR);
> + builder =3D (struct env_builder){0};
> + env_builder_destroy(&builder);
> + env_free(builder.values);
> +}
> +
> +int main(void) { check_environment(); puts("resource regression tests pa=
ssed"); return 0; }
> --- /dev/null
> +++ b/tests/budget_fixture.h
> @@ -0,0 +1,18 @@
> +/* Intentionally independent of cron_limits.h: removing parser accountin=
g must
> + * make the above-budget cases fail. Historical replacements count, alth=
ough
> + * only one 900-byte variable is live. Both fixtures are well below file=
/line
> + * limits; 50 copies cost <16 MiB and 90 copies cost >16 MiB. */
> +static void
> +write_budget_fixture(FILE *file, int system, int jobs)
> +{
> + int i;
> + char value[901];
> + memset(value, 'x', sizeof value - 1);
> + value[sizeof value - 1] =3D '\0';
> + fprintf(file, "* * * * * %strue\n", system ? "root " : "");
> + for (i =3D 0; i < 256; i++)
> + fprintf(file, "REPLACED=3D%s\n", value);
> + for (i =3D 0; i < jobs; i++)
> + fprintf(file, "* * * * * %strue\n", system ? "root " : "");
> + assert(ftell(file) < MAX_CRONTAB_BYTES / 2);
> +}
> --- /dev/null
> +++ b/tests/parser_environment.c
> @@ -0,0 +1,90 @@
> +#define MAIN_PROGRAM
> +#include "../cron.h"
> +#include <assert.h>
> +#include "budget_fixture.h"
> +
> +static user *
> +load_fixture(FILE *file, struct passwd *pw)
> +{
> + int fd;
> + assert(fflush(file) =3D=3D 0);
> + rewind(file);
> + fd =3D dup(fileno(file));
> + assert(fd >=3D 0);
> + return load_user(fd, pw, "root", "*system*environment", "environment");
> +}
> +
> +static void
> +check_budget(struct passwd *pw)
> +{
> + int cases[] =3D {50, 90, 50};
> + size_t i;
> + for (i =3D 0; i < sizeof cases / sizeof cases[0]; i++) {
> + FILE *file =3D tmpfile();
> + user *u;
> + assert(file !=3D NULL);
> + write_budget_fixture(file, pw =3D=3D NULL, cases[i]);
> + u =3D load_fixture(file, pw);
> + fclose(file);
> + if (cases[i] =3D=3D 90) {
> + /* No partially parsed jobs may escape rejection. */
> + assert(u =3D=3D NULL);
> + } else {
> + entry *e;
> + int count =3D 0;
> + assert(u !=3D NULL);
> + for (e =3D u->crontab; e; e =3D e->next)
> + count++;
> + assert(count =3D=3D cases[i] + 1);
> + free_user(u);
> + }
> + }
> +}
> +
> +static void
> +check_snapshots(struct passwd *pw)
> +{
> + FILE *file =3D tmpfile();
> + user *u;
> + entry *first, *second, *third;
> + const char *owner =3D pw ? "" : "root ";
> + struct passwd *expected =3D pw ? pw : getpwnam("root");
> + assert(file && expected);
> + fprintf(file, "* * * * * %sfirst\n", owner);
> + fprintf(file, "VALUE=3Dold\nEMPTY=3Dfilled\nPATH=3D/custom\n* * * * * %=
ssecond\n", owner);
> + fprintf(file, "VALUE=3Dnew\nEMPTY=3D\n* * * * * %sthird\n", owner);
> + u =3D load_fixture(file, pw);
> + fclose(file);
> + assert(u && (third =3D u->crontab) && (second =3D third->next) && (firs=
t =3D second->next));
> + assert(first->next =3D=3D NULL);
> + assert(strcmp(first->cmd, "first") =3D=3D 0);
> + assert(strcmp(second->cmd, "second") =3D=3D 0);
> + assert(strcmp(third->cmd, "third") =3D=3D 0);
> + assert(env_get("VALUE", first->envp) =3D=3D NULL);
> + assert(env_get("EMPTY", first->envp) =3D=3D NULL);
> + assert(strcmp(env_get("VALUE", second->envp), "old") =3D=3D 0);
> + assert(strcmp(env_get("EMPTY", second->envp), "filled") =3D=3D 0);
> + assert(strcmp(env_get("VALUE", third->envp), "new") =3D=3D 0);
> + assert(strcmp(env_get("EMPTY", third->envp), "") =3D=3D 0);
> + assert(strcmp(env_get("PATH", first->envp), _PATH_DEFPATH) =3D=3D 0);
> + assert(strcmp(env_get("PATH", second->envp), "/custom") =3D=3D 0);
> + assert(strcmp(env_get("PATH", third->envp), "/custom") =3D=3D 0);
> + for (; third; third =3D third->next) {
> + assert(strcmp(env_get("SHELL", third->envp), _PATH_BSHELL) =3D=3D 0);
> + assert(strcmp(env_get("HOME", third->envp), expected->pw_dir) =3D=3D 0=
);
> + assert(strcmp(env_get("LOGNAME", third->envp), expected->pw_name) =3D=
=3D 0);
> + }
> + free_user(u);
> +}
> +
> +int main(void)
> +{
> + struct passwd *pw =3D getpwuid(getuid());
> + assert(pw !=3D NULL);
> + check_budget(pw);
> + check_snapshots(pw);
> + check_budget(NULL);
> + check_snapshots(NULL);
> + puts("production loader environment budget and snapshot tests passed");
> + return 0;
> +}
--OjoGCRX0Gaxw+7po
Content-Type: application/pgp-signature; name="signature.asc"
-----BEGIN PGP SIGNATURE-----
iQIzBAABCAAdFiEEM0CzZP9nFT+3zK6FHCgWkHE2rjkFAmrqEscACgkQHCgWkHE2
rjmLEA/+JMFWlLRZJZ7PZsVbTHzRjPC6ki6B1bmrWwRnGzH3DV0ySH2hU4dZ717u
naI3ZkYyxP1SqY60lwjJ22HH4LrU9Yju2Ux7UmhspscxMzthG67ofR6N3kVWyaQp
AolKfIubWQnjaVZDjN8H5R6j9TQyupjkS0mjxE77PZeQEfJ2JdHOceAXo98PSbIE
fniIVJpqN22nY6XgJVDr5w2vIVMZ89IQJcZRzfT0xJIKr69IaaELVTxX1+VZQpQT
GLpLZcKjp/1elIgHKY4/nyZuxCjIQcDgIAqdjKMLpPuHCtxVONt+4vK625iOCVoq
Gv3/FZBcbZY3s6taqRxdys+4D/g9gWbE92ms77KUSwegzvxbPlqaYTa+TDmeNObI
MkI1V+yvAo/8Hl08EKPTXgqdw1kMZDx7hs1kIo7XFVl45Ox3z0DUklNxSBcISHvg
Sfg1pnfyreZNoHUiJb2AXQr6AiHQUtSMTpYflE6f3Lmihdu1lphAWWOnuJfS01JY
bPQzDnP6BYWld1xDQEzXZDMT8OYHrZ7TpsfS8YyQsqlotUhW5g7ua5XwBG6MATRX
FJvdvZ24B9cRc4BhKJps1rx9oHdKB5isREgePWgvIFzKvsW30nR1XCq5Z9uzsVe7
q5iWBOj6Zij6/rAMVeQKSm6EK81v2CH5qkkh3ATCbBoX1tjXR6s=
=Bhzk
-----END PGP SIGNATURE-----
--OjoGCRX0Gaxw+7po--
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads