Skip to content
DoS Advisory Beginner

GHSL-2024-323: Denial of Service (DoS) in snapcraft

A regular expression used to validate time values in snapcraft's YAML configuration is vulnerable to catastrophic backtracking (ReDoS). An attacker could supply a crafted snap with a malicious time value to cause the tool to hang, leading to a denial of service.

kevinbackhouse Follow

Originally disclosed on GitHub Security Lab

validatetime uses a regex to check that time values in a snap’s yaml file are formatted correctly:

kevinbackhouse, on GitHub Security Lab

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 3 min read.

Context

snapcraft is the command line tool used to build snaps. A snap is described by a YAML file, snap/snapcraft.yaml, which declares the snap's name, base, apps and their runtime settings. Among those settings are time values such as start_timeout, stop_timeout, watchdog_timeout and restart_delay. snapcraft validates these values when it loads the project file, so that a malformed duration is rejected before anything is built.

The validation is done with a regular expression in _validate_time, a pydantic field validator in snapcraft/models/project.py. The report shows that this regex is vulnerable to a regular expression denial of service (ReDoS): a crafted YAML file makes the validator run for an exponentially growing amount of time as the invalid input gets longer.

Setup

The issue was tested against snapcraft 8.4.4. No special account or role is needed: the only requirements are a working snapcraft installation and the ability to run it against a project directory. The researcher created an empty snap with snapcraft init, replaced the generated YAML, and ran snapcraft to observe the delay. The bug was found with the help of CodeQL's Inefficient regular expression query.

Finding it

The validator uses a regex to check that time values are formatted correctly:

Python
@pydantic.field_validator(
    "start_timeout", "stop_timeout", "watchdog_timeout", "restart_delay"
)
@classmethod
def _validate_time(cls, timeval):
    if not re.match(r"^[0-9]+(ns|us|ms|s|m)*$", timeval):
        raise ValueError(f"{timeval!r} is not a valid time value")

    return timeval

Valid values such as 10s or 10ms match quickly. The problem is the structure of the pattern: a run of digits followed by a group of alternative unit suffixes that can repeat any number of times, with no anchoring of the suffix group to a single choice. On an invalid input the engine has many ways to divide the same characters between repetitions of the group, and it explores them all before it can conclude that the match fails. The researcher's example of a slow input is:

[redacted]

This is a valid-looking prefix (0 followed by repeated ms) that ends in x, so the overall match must fail, and the failing path is the expensive one.

Exploitation

Step 1: create an empty snap

Bash
snapcraft init

This generates the project skeleton, including ./snap/snapcraft.yaml.

Step 2: replace the YAML with a malicious project

The contents of ./snap/snapcraft.yaml are replaced with:

name: GHSL-2024-323
base: core24

apps:
  GHSL-2024-323:
    command: /bin/echo kevwozere
    restart-delay: [redacted]

The restart-delay value is the crafted string: a digit, many repetitions of ms, and a trailing x that makes the value invalid. Because restart_delay is one of the fields covered by _validate_time, loading the project sends this string straight into the vulnerable regex.

Step 3: run snapcraft and observe the delay

Bash
snapcraft

The command takes a long time to finish. The running time increases exponentially with the length of the invalid string.

Root cause

The regex ^[0-9]+(ns|us|ms|s|m)*$ has a ReDoS vulnerability, so it runs very slowly on invalid inputs. When the input fails to match, the engine backtracks through the possible ways of splitting the repeated suffixes, and the number of combinations grows exponentially with input length. The validator is applied to values taken directly from the project's YAML file, so the attacker controls the exact string that reaches the regex.

Impact

A snap with a crafted YAML file can cause a denial of service in snapcraft: the tool stalls while validating the project. The researcher tested the proof of concept only locally, but notes that it may also be possible to trigger a denial of service on https://snapcraft.io/ by uploading a malicious snap. That server-side scenario was not tested, so it remains a possibility rather than a demonstrated result.

Fix

No response was received to the report, so on 2025-01-20 the researcher created a public pull request to fix the bug: https://github.com/canonical/snapcraft/pull/5210.

Variants to test

Hunting notes

The signal is a regex that accepts a family of valid values but has no length limit on the input.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 10 min read.

What happened, in plain words

A security researcher found a way to make a tool called snapcraft freeze for a very long time by giving it a small, deliberately malformed text file. The tool is used to build snaps, which are packages of software for Linux systems. When snapcraft reads the file that describes a snap, it checks that certain time settings look sensible. That check is done with a regular expression (a pattern-matching tool, explained below), and the pattern is written in a way that can be made to run extremely slowly. The researcher wrote a short file that triggers this slowdown, ran snapcraft on it, and watched the command take a very long time to finish. The longer the bad text, the worse the slowdown grows exponentially. This kind of attack is called a denial of service: the software is not broken into or stolen from, it is simply made too busy to do its job.

The researcher reported the problem to the snapcraft project on 2024-11-04, asked for an update on 2025-01-09, and, after getting no response by 2025-01-20, opened a public pull request with a fix. The report was published on 2025-01-23.

Context

What snapcraft and snaps are

A snap is a self-contained software package. Think of it like a sealed box that holds a program plus everything the program needs to run, so it behaves the same on different machines. snapcraft is the command-line tool that builds these boxes. A command-line tool is a program you run by typing its name in a terminal window, rather than clicking buttons in a graphical app.

Every snap is described by a text file written in YAML. YAML is a plain-text format for structured data: it uses indentation and simple key: value lines to describe settings, a bit like a very tidy shopping list where each item can have sub-items. For snapcraft the file lives at snap/snapcraft.yaml and declares things such as the snap's name, its base (the underlying system version it is built on), the apps it contains, and runtime settings for those apps.

Among those runtime settings are time values: start_timeout, stop_timeout, watchdog_timeout and restart_delay. A timeout is how long the system waits for something before giving up; a restart delay is how long it waits before restarting a crashed program. These are written as a number followed by a unit, for example 10s for ten seconds or 10ms for ten milliseconds.

When snapcraft loads the project file, it validates these values: it checks they are well formed and rejects a malformed duration before anything is built. Validation is like a bouncer at a door checking tickets before anyone gets inside; catching a bad value early avoids confusing failures later.

The check and its weakness

The validation is done with a regular expression inside a function called _validate_time, which lives in the file snapcraft/models/project.py. A regular expression (often shortened to regex) is a small pattern language for describing what a piece of text should look like. You have probably met the idea without the name: a rule like "three digits, then a dash, then four digits" for a phone number is the same kind of thinking. Software uses regexes to answer the question "does this text match the pattern?"

The report shows that this particular regex is vulnerable to a regular expression denial of service, usually shortened to ReDoS. That means a crafted piece of text can make the pattern-matching engine work for an enormously long time before it decides the text does not match. The longer the crafted text, the worse it gets.

Setup

The issue was tested against snapcraft version 8.4.4. No special account or role is needed: the only requirements are a working snapcraft installation and the ability to run it against a project directory. The researcher created an empty snap with snapcraft init, replaced the generated YAML file, and ran snapcraft to observe the delay. The bug was found with the help of CodeQL's Inefficient regular expression query. CodeQL is a tool that scans source code for patterns that commonly lead to bugs; its "Inefficient regular expression" query specifically looks for regexes that can be made to run very slowly.

Finding it

The validator uses a regex to check that time values are formatted correctly:

Python
@pydantic.field_validator(
    "start_timeout", "stop_timeout", "watchdog_timeout", "restart_delay"
)
@classmethod
def _validate_time(cls, timeval):
    if not re.match(r"^[0-9]+(ns|us|ms|s|m)*$", timeval):
        raise ValueError(f"{timeval!r} is not a valid time value")

    return timeval

Line by line, in plain words:

  1. @pydantic.field_validator(...) is a label placed above the function that tells a library called pydantic (a tool for checking data shapes in Python) to run this function automatically whenever one of the named fields is loaded. The four names listed are the time settings: start_timeout, stop_timeout, watchdog_timeout and restart_delay. So every time snapcraft reads one of those settings from the YAML file, this function is called with the value.
  2. @classmethod is another label telling Python how the function belongs to its class; it is plumbing, not part of the security story.
  3. def _validate_time(cls, timeval): defines the function. timeval is the value being checked, for example the text 10s.
  4. if not re.match(r"^[0-9]+(ns|us|ms|s|m)*$", timeval): is the heart of it. re.match asks the regex engine whether the text fits the pattern. The pattern is written as a raw string (the r before the quotes), which just means backslashes are taken literally. Reading the pattern from left to right:
    • ^ means "start of the text".
    • [0-9]+ means "one or more digits".
    • (ns|us|ms|s|m)* means "zero or more repetitions of one of these units: ns, us, ms, s, m". The vertical bars mean "or".
    • $ means "end of the text". So the whole pattern says: digits, then any number of unit suffixes, and nothing else.
  5. raise ValueError(...) runs when the text does not match: it stops the program with an error message saying the value is not a valid time.
  6. return timeval hands the value back unchanged when it does match.

Valid values such as 10s or 10ms match quickly. The problem is that the pattern has a ReDoS vulnerability, so it runs very slowly on invalid inputs. On an invalid input the engine has many ways to divide the same characters between repetitions of the group, and it explores them all before it can conclude that the match fails. A useful comparison is a maze with many branching corridors: to prove there is no exit, you may have to walk every branch. The running time increases exponentially with the length of the invalid string.

The researcher's example of a slow input is:

[redacted]

This is a valid-looking prefix (0 followed by repeated ms) that ends in x, so the overall match must fail, and the failing path is the expensive one. The trailing x makes the value invalid, so the overall match must fail.

Exploitation

Step 1: create an empty snap

Bash
snapcraft init

snapcraft init is the command that sets up a fresh, empty snap project in the current folder. It generates the project skeleton, including the file ./snap/snapcraft.yaml.

Step 2: replace the YAML with a malicious project

The contents of ./snap/snapcraft.yaml are replaced with:

name: GHSL-2024-323
base: core24

apps:
  GHSL-2024-323:
    command: /bin/echo kevwozere
    restart-delay: [redacted]

Line by line:

  1. name: GHSL-2024-323 sets the snap's name. The researcher used the identifier of the bug report as the name, which is just a label.
  2. base: core24 says which underlying system version the snap is built on. core24 is the name of that base.
  3. apps: begins the section that lists the programs inside the snap.
  4. GHSL-2024-323: is the name of one app, indented under apps:. In YAML, indentation shows what belongs to what, like nested bullet points.
  5. command: /bin/echo kevwozere is the command the app runs. /bin/echo is a standard program that prints its arguments, so this would print the word kevwozere.
  6. restart-delay: [redacted] is the crafted value. It is a digit 0, many repetitions of ms, and a trailing x that makes the value invalid. Because restart_delay is one of the fields covered by _validate_time, loading the project sends this string straight into the vulnerable regex. Note that the YAML spelling uses a hyphen (restart-delay) while the code uses an underscore (restart_delay); the two refer to the same setting.

Step 3: run snapcraft and observe the delay

Bash
snapcraft

Because building starts by loading and validating the project file, the crafted restart-delay value is checked immediately, and the command takes a long time to finish. Exponential growth is the difference between adding one more step and doubling the whole staircase: a few extra characters turn a moment into minutes.

Root cause

The regex ^[0-9]+(ns|us|ms|s|m)*$ has a ReDoS vulnerability, so it runs very slowly on invalid inputs. When the input fails to match, the engine backtracks through the possible ways of splitting the repeated suffixes, and the number of combinations grows exponentially with input length. Backtracking is the engine trying one interpretation, hitting a dead end, and going back to try another, like someone retracing their steps in that branching maze.

The validator is applied to values taken directly from the project's YAML file, so the attacker controls the exact string that reaches the regex.

Impact

A snap with a crafted YAML file can cause a denial of service in snapcraft: the tool stalls while validating the project. The researcher tested the proof of concept only locally, but notes that it may also be possible to trigger a denial of service on https://snapcraft.io/ by uploading a malicious snap. That server-side scenario was not tested, so it remains a possibility rather than a demonstrated result. A proof of concept (PoC) is a small demonstration that shows a problem is real without causing harm.

Fix

No response was received to the report, so on 2025-01-20 the researcher created a public pull request to fix the bug: https://github.com/canonical/snapcraft/pull/5210. A pull request is a proposed change to a code repository that others can review and merge.

Variants to test

These are other places where the same kind of weakness might appear:

Hunting notes

Recon (short for reconnaissance) means the early survey of a target to see what it is made of. The signal is a regex that accepts a family of valid values but has no length limit on the input.

Glossary

  • Backtracking: the regex engine's habit of trying one interpretation, hitting a dead end, and going back to try another.
  • Base: in snapcraft, the underlying system version a snap is built on, such as core24.
  • CodeQL: a tool that scans source code for patterns that commonly lead to bugs.
  • Command-line tool: a program you run by typing its name in a terminal rather than clicking in a graphical app.
  • Denial of service (DoS): making software too busy or too slow to do its job, without breaking into it.
  • Exponential growth: growth where each extra unit roughly multiplies the total work, rather than adding a fixed amount.
  • Field validator: a function that automatically checks a particular setting when data is loaded.
  • Manifest: a file that describes a project's contents and settings.
  • Pydantic: a Python library for checking that data has the expected shape.
  • Proof of concept (PoC): a small demonstration that shows a problem is real without causing harm.
  • Pull request: a proposed change to a code repository that others can review and merge.
  • Raw string: a string in Python written with an r prefix, where backslashes are taken literally.
  • Recon: short for reconnaissance, the early survey of a target to see what it is made of.
  • Regex (regular expression): a small pattern language for describing what a piece of text should look like.
  • ReDoS (regular expression denial of service): a denial of service caused by a crafted text that makes a regex engine work for an enormously long time.
  • Snap: a self-contained software package for Linux that includes a program and what it needs to run.
  • snapcraft: the command-line tool used to build snaps.
  • Timeout: how long a system waits for something before giving up.
  • Validation: checking that data is well formed before using it.
  • YAML: a plain-text format for structured data, using indentation and key: value lines.

More by kevinbackhouse

View all

47 more disclosed reports, best first.

More from GitHub Security Lab

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.