|
667 | 667 | }); |
668 | 668 | }); |
669 | 669 |
|
| 670 | + describe("harden_resource_limits", () => { |
| 671 | + // Shim `ulimit` (a bash builtin) by overriding it with a function inside the |
| 672 | + // sourced body. The function records each invocation so we can assert both |
| 673 | + // the nproc (#809) and nofile (#4527) caps are applied, soft-before-hard. |
| 674 | + it("applies nproc and nofile soft+hard limits in order", () => { |
| 675 | + const { stdout } = runWithLib( |
| 676 | + [ |
| 677 | + // Override the ulimit builtin to record args and succeed. |
| 678 | + "ulimit() { printf 'ulimit %s\\n' \"$*\"; return 0; }", |
| 679 | + "harden_resource_limits", |
| 680 | + ].join("\n"), |
| 681 | + ); |
| 682 | + const calls = stdout.split("\n").filter((line) => line.startsWith("ulimit ")); |
| 683 | + expect(calls).toEqual([ |
| 684 | + "ulimit -Su 512", |
| 685 | + "ulimit -Hu 512", |
| 686 | + "ulimit -Sn 65536", |
| 687 | + "ulimit -Hn 65536", |
| 688 | + ]); |
| 689 | + }); |
| 690 | + |
| 691 | + it("is best-effort: exits 0 and warns when ulimit fails", () => { |
| 692 | + // Shim ulimit to always fail. The function must not abort (best-effort) |
| 693 | + // and must emit a [SECURITY] warning for each of the four limits. |
| 694 | + const { stdout } = runWithLib( |
| 695 | + [ |
| 696 | + "ulimit() { return 1; }", |
| 697 | + "harden_resource_limits 2>&1", |
| 698 | + 'echo "HARDEN_OK"', |
| 699 | + ].join("\n"), |
| 700 | + ); |
| 701 | + expect(stdout).toContain("HARDEN_OK"); |
| 702 | + expect(stdout).toContain("Could not set soft nproc limit"); |
| 703 | + expect(stdout).toContain("Could not set hard nproc limit"); |
| 704 | + expect(stdout).toContain("Could not set soft nofile limit"); |
| 705 | + expect(stdout).toContain("Could not set hard nofile limit"); |
| 706 | + }); |
| 707 | + }); |
| 708 | + |
| 709 | + describe("entrypoints call harden_resource_limits", () => { |
| 710 | + // Both entrypoints must delegate RLIMIT hardening to the shared helper and |
| 711 | + // must no longer carry the pre-#4527 raw inline `ulimit -Su 512` block. |
| 712 | + for (const rel of ["../scripts/nemoclaw-start.sh", "../agents/hermes/start.sh"]) { |
| 713 | + it(`${rel} calls harden_resource_limits and has no raw inline nproc block`, () => { |
| 714 | + const src = readFileSync(join(import.meta.dirname, rel), "utf-8"); |
| 715 | + expect(src).toContain("harden_resource_limits"); |
| 716 | + expect(src).not.toContain("ulimit -Su 512"); |
| 717 | + expect(src).not.toContain("ulimit -Hu 512"); |
| 718 | + }); |
| 719 | + } |
| 720 | + |
| 721 | + // SECURITY (#4527): the RLIMIT caps are only unraisable if they are set |
| 722 | + // while still root PID 1, BEFORE drop_capabilities (capsh) and the |
| 723 | + // setpriv/gosu step-down. A refactor that moved the harden call after the |
| 724 | + // privilege drop would turn it into dead code (cap set as the unprivileged |
| 725 | + // agent, hard limit no longer lowered) while every other test stayed green. |
| 726 | + // Pin the ordering so that regression is caught. |
| 727 | + for (const rel of ["../scripts/nemoclaw-start.sh", "../agents/hermes/start.sh"]) { |
| 728 | + it(`${rel} calls harden_resource_limits before drop_capabilities`, () => { |
| 729 | + const src = readFileSync(join(import.meta.dirname, rel), "utf-8"); |
| 730 | + // Anchor to executable command lines, not free-text, so a comment |
| 731 | + // mentioning either name cannot satisfy (or break) the ordering check. |
| 732 | + const hardenIdx = src.match(/^\s*harden_resource_limits\s*$/m)?.index ?? -1; |
| 733 | + const dropIdx = src.match(/^\s*drop_capabilities\b.*$/m)?.index ?? -1; |
| 734 | + expect(hardenIdx).toBeGreaterThanOrEqual(0); |
| 735 | + expect(dropIdx).toBeGreaterThanOrEqual(0); |
| 736 | + expect(hardenIdx).toBeLessThan(dropIdx); |
| 737 | + }); |
| 738 | + } |
| 739 | + }); |
| 740 | + |
670 | 741 | describe("init_step_down_prefixes", () => { |
671 | 742 | it("falls back to gosu when setpriv is unavailable", () => { |
672 | 743 | // Source-time init runs before our test body, so re-run it with a |
|
832 | 903 | it("nemoclaw-start.sh sources sandbox-init.sh", () => { |
833 | 904 | const src = readFileSync(join(import.meta.dirname, "../scripts/nemoclaw-start.sh"), "utf-8"); |
834 | 905 | const start = src.indexOf("_SANDBOX_INIT="); |
835 | | - const end = src.indexOf("# Harden: limit process count", start); |
| 906 | + // Bound the source block at the harden_resource_limits call line itself |
| 907 | + // (executable, stable) rather than a free-text comment that may be reworded. |
| 908 | + const hardenCallFromStart = src.slice(start).match(/^\s*harden_resource_limits\s*$/m); |
| 909 | + const end = hardenCallFromStart ? start + (hardenCallFromStart.index ?? 0) : -1; |
836 | 910 | if (start === -1 || end === -1 || end <= start) { |
837 | 911 | throw new Error("Expected sandbox-init source block in scripts/nemoclaw-start.sh"); |
838 | 912 | } |
|
0 commit comments