Skip to content

V: enable stack usage guard#4387

Draft
masatake wants to merge 14 commits into
universal-ctags:masterfrom
masatake:V--CVE-2026-2641
Draft

V: enable stack usage guard#4387
masatake wants to merge 14 commits into
universal-ctags:masterfrom
masatake:V--CVE-2026-2641

Conversation

@masatake
Copy link
Copy Markdown
Member

@masatake masatake commented Feb 21, 2026

Fixes #4369 (CVE-2026-2641)
Fixes #4409

GHSA-69fg-c96p-c6fq

Author: Masatake YAMATO [email protected]
Date: Fri Feb 20 04:23:04 2026 +0900

V: enable stack usage guard

Enable the stack usage guard in the V parser.

Partially fixes: #4369
Partially addresses: CVE-2026-2641

The default stack limit is 4MB.
It may need adjustment on platforms with smaller default
stack sizes. (WIP)

Signed-off-by: Masatake YAMATO <[email protected]>

Author: Masatake YAMATO [email protected]
Date: Fri Feb 20 04:22:45 2026 +0900

main: add stack usage guard

Introduce a stack usage guard to prevent stack overflow
in deeply nested input.
as
The default stack limit is currently 4MB.
It can be adjusted with the --stack-limit=<bytes> option.

This change adds the infrastructure in the main part.
Individual parsers must explicitly enable the guard
to benefit from stack protection.

Signed-off-by: Masatake YAMATO <[email protected]>

TODO:

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 23, 2026

Codecov Report

❌ Patch coverage is 79.16667% with 30 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.97%. Comparing base (76e4cc2) to head (f253e51).

Files with missing lines Patch % Lines
main/stackguard.c 88.46% 9 Missing ⚠️
parsers/jscript.c 25.00% 9 Missing ⚠️
main/options.c 80.00% 5 Missing ⚠️
parsers/v.c 28.57% 5 Missing ⚠️
main/entry.c 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4387      +/-   ##
==========================================
- Coverage   85.98%   85.97%   -0.02%     
==========================================
  Files         255      256       +1     
  Lines       63884    64023     +139     
==========================================
+ Hits        54931    55042     +111     
- Misses       8953     8981      +28     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Enables an opt-in stack usage guard to mitigate stack overflows during parsing (notably for the V parser), and adds a CLI option to configure the guard’s limit.

Changes:

  • Add stack guard infrastructure (main/stackguard.c, main/stackguard_p.h) and wire it into the parse loop via per-parser discardInput.
  • Enable the guard in the V parser and expose --stack-limit=<bytes> plus diagnostics (verbose + totals).
  • Update build files (make + VS project) and adjust expected outputs for updated diagnostics.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
win32/ctags_vs2013.vcxproj.filters Adds stackguard sources/headers to VS filters for building on Windows.
win32/ctags_vs2013.vcxproj Includes stackguard source/header in the VS project build.
source.mak Adds stackguard source/header to the build lists for non-Windows builds.
parsers/v.c Enables stack guard checks in an expression list parse path; provides parser-specific EOF discard function.
main/stats.c Prints observed peak stack usage in totals output.
main/stackguard_p.h Declares the stack guard API used by main/options/parse/stats.
main/stackguard.c Implements stack tracking, limit handling, and peak usage tracking.
main/parse_p.h Declares the query function for stack-guard support (typo present).
main/parse.h Adds discardInputFn hook and exposes stackGuardCheck to parsers.
main/parse.c Prepares/releases stack guard around parser execution; adds support query function (typo present).
main/options.c Adds --stack-limit option and prints “stack guard supported/unsupported” in language description; updates some error messages.
main/main.c Logs the computed stack limit in verbose mode.
configure.ac Detects sys/resource.h for getrlimit-based default limit computation.
Tmain/versioning.d/stdout-expected.txt Updates expected “describe language” output for “stack guard” line.
Tmain/broken-tagname.d/stderr-expected.txt Updates expected verbose output to include stack limit line.
Tmain/broken-tagname.d/run.sh Adds --stack-limit=8192 to stabilize verbose output and adjusts sed formatting.
Tmain/broken-tagname-in-ectags-format.d/stderr-expected.txt Updates expected verbose output to include stack limit line.
Tmain/broken-tagname-in-ectags-format.d/run.sh Adds --stack-limit=8192 to stabilize verbose output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread main/stackguard.c Outdated
Comment thread main/stackguard.c Outdated
Comment thread main/stackguard.c Outdated
Comment thread main/options.c Outdated
Comment thread main/options.c
Comment thread main/stackguard.c Outdated
Comment thread main/parse_p.h Outdated
Comment thread main/stackguard_p.h Outdated
Comment thread main/parse.h Outdated
Comment thread parsers/v.c
Comment on lines +2344 to +2345
if (!stackGuardCheck(token))
return;
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces new control-flow behavior (early return + EOF discard) when the stack limit is exceeded, but the PR description lists 'add test cases' as TODO and the current diffs only stabilize verbose output tests. Add an integration test that forces deep nesting in V input (or a dedicated selftest parser case) to assert: (1) a NOTICE is emitted about exceeding the limit, and (2) parsing terminates safely without crashing/hanging.

Copilot uses AI. Check for mistakes.
@masatake masatake force-pushed the V--CVE-2026-2641 branch 3 times, most recently from f70ab53 to 7cd2d3a Compare February 26, 2026 16:19
@masatake masatake force-pushed the V--CVE-2026-2641 branch 4 times, most recently from 08e746e to dc4393b Compare March 22, 2026 23:25
@masatake masatake marked this pull request as draft March 30, 2026 08:51
@masatake masatake force-pushed the V--CVE-2026-2641 branch 2 times, most recently from a038815 to 5df0ca9 Compare May 3, 2026 21:16
@masatake masatake force-pushed the V--CVE-2026-2641 branch 3 times, most recently from 56ee44b to 2043dca Compare May 6, 2026 21:49
@masatake
Copy link
Copy Markdown
Member Author

masatake commented May 6, 2026

The DTS parser looks broken.
When processing the Linux source tree, 32, the limit defined in 7a35227 causes many notice messages.

@masatake
Copy link
Copy Markdown
Member Author

masatake commented May 6, 2026

ulimit based test may not work on Windows.

masatake added 2 commits May 7, 2026 19:09
Introduce a stack usage guard to prevent stack overflow
in deeply nested input.

The default stack limit is derived from the value
returned by getrlimit(RLIMIT_STACK). On platforms where
getrlimit() is not available, a default limit of about
1MB is used, with a 32kB safety margin reserved.

The limit can be adjusted with the --stack-limit=<bytes>
option.

The --verbose option prints the stack limit actually used:

  $ ./ctags --help
  ...
   --stack-limit=<bytes>
       Limit the stack usage while parsing, in bytes [8372224].

  $ ./ctags --stack-limit=81920 --help
  ...
   --stack-limit=<bytes>
       Limit the stack usage while parsing, in bytes [81920].
  ...

The --totals option prints the observed peak stack usage:

  $ ./ctags --totals -o - main/main.c > /dev/null
  1 file, 612 lines (13 kB) scanned in 0.0 seconds (4710 kB/s)
  20 tags added to tag file
  20 tags sorted in 0.00 seconds
  Observed peak stack usage: 4096 bytes (4.0 KiB)

This change introduces the infrastructure in the main
part. Individual parsers must explicitly enable the guard
to benefit from stack protection.

Signed-off-by: Masatake YAMATO <[email protected]>
@masatake masatake force-pushed the V--CVE-2026-2641 branch 2 times, most recently from 4bf2fdd to 6fa47df Compare May 7, 2026 13:55
masatake added 5 commits May 8, 2026 04:35
Partially fixes: universal-ctags#4369
Partially addresses: CVE-2026-2641

Signed-off-by: Masatake YAMATO <[email protected]>
The stack guard limits the recursion in parsing.
I found the stack guard is not enough to use ctags with bad input.

If parser->requestAutomaticFQTag is enabled, ctags fills scope field
of tags automatically based on cork indexes signed to scopeIndex
member of tagEntryInfo. If the scope is too deep, filling takes too
long time.

This change limits the depth of scope to 32.
Unlike the stack guard, I hard-coded to limit.

Signed-off-by: Masatake YAMATO <[email protected]>
@masatake masatake force-pushed the V--CVE-2026-2641 branch from 6fa47df to 09484ef Compare May 7, 2026 19:37
@masatake
Copy link
Copy Markdown
Member Author

masatake commented May 7, 2026

Cygwin:

/home/runner/work/ctags/ctags/Tmain/stack-guard-option.d/stdout-diff.txt

	--- ./Tmain/stack-guard-option.d/stdout-expected.txt	2026-05-07 19:40:59.220998901 +0000
	+++ /home/runner/work/ctags/ctags/Tmain/stack-guard-option.d/stdout-actual.txt	2026-05-07 19:42:54.339347000 +0000
	@@ -3,2 +2,0 @@
	-Notice: call stack exceeds the limit (LIMIT) during processing input: input.v
	-qnCmeoduen	input.v	/^moluleptt b); assi9223372036854775n b const qnCmeoduen=$/;"	c
	@@ -11,2 +8,0 @@
	-Notice: call stack exceeds the limit (LIMIT) during processing input: input.v
	-qnCmeoduen	input.v	/^moluleptt b); assi9223372036854775n b const qnCmeoduen=$/;"	c
	@@ -19 +14,0 @@
	-Warning: don't specify stack limit larger than default: 32769 > 32768
	@@ -22,0 +18,4 @@
	+# ulimit(HELP): 64, no option
	+  --stack-limit=<bytes>
	+       Limit the stack usage while parsing, in bytes [18446744073709518847].
	+
	@@ -24,2 +22,0 @@
	-Notice: call stack exceeds the limit (LIMIT) during processing input: input.js
	-f0	input.js	/^var f0 = function(LIMIT) {$/;"	f

/home/runner/work/ctags/ctags/Tmain/stack-guard-option.d/stderr-diff.txt

	--- ./Tmain/stack-guard-option.d/stderr-expected.txt	2026-05-07 19:40:59.220998901 +0000
	+++ /home/runner/work/ctags/ctags/Tmain/stack-guard-option.d/stderr-actual.txt	2026-05-07 19:42:54.339376000 +0000
	@@ -0,0 +1,3 @@
	+Segmentation fault (core dumped)
	+Segmentation fault (core dumped)
	+Segmentation fault (core dumped)

FreeBSD:

	--- ./Tmain\stack-guard-option.d/stdout-expected.txt	2026-05-07 19:41:48.388036100 +0000
	+++ D:/a/ctags/ctags/Tmain/stack-guard-option.d/stdout-actual.txt	2026-05-07 19:48:39.894513000 +0000
	@@ -19 +18,0 @@
	-Warning: don't specify stack limit larger than default: 32769 > 32768
	@@ -21,0 +21,4 @@
	+
	+# ulimit(HELP): 64, no option
	+  --stack-limit=<bytes>
	+       Limit the stack usage while parsing, in bytes [2048000].


ctags on both platforms use getrlimit.

Comment thread main/stackguard.c
{
rlim_t cur = rlim.rlim_cur;

if (rlim.rlim_cur == RLIM_INFINITY || cur > (rlim_t) SIZE_MAX)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way to handle RLIM_INFINITY is completely wrong.
If a user specifies RLIM_INFINITY (via ulimit -s), ctags should disable the stack usage guard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JavaScript parser can crash or exhaust memory with deeply nested functions [Bug] Stack-overflow in V parser (parseExpression/parseExprList)

2 participants