[PR #14176] fs/gguf: close file on parsing errors in Open #40425

Open
opened 2026-04-23 01:19:19 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/14176
Author: @kolega-ai-dev
Created: 2/9/2026
Status: 🔄 Open

Base: mainHead: v11-finding_26


📝 Commits (1)

  • 472ade3 fs/gguf: close file on parsing errors in Open

📊 Changes

1 file changed (+6 additions, -0 deletions)

View changed files

📝 fs/gguf/gguf.go (+6 -0)

📄 Description

Vulnerability identified and fix provided by Kolega.dev

File Descriptor Leak on Parsing Errors

Location

fs/gguf/gguf.go:47-91

Description

The Open() function opens a file with os.Open() but fails to close it on multiple error return paths. If magic number validation, version check, or lazy loader initialization fails, the opened file descriptor is leaked.

GGUF files can be uploaded by users via the /api/create endpoint. An attacker could repeatedly upload malformed GGUF files that fail validation checks, causing file descriptor exhaustion and denial of service. File descriptors are a finite system resource.

Analysis Notes

The Open() function at lines 47-91 opens a file with os.Open() at line 49 but fails to close it on error paths at lines 57, 61, 65, 69, 74, and 87. All callers (e.g. server/images.go:79) properly call defer f.Close() on success, but the error paths in Open itself were not covered.

Fix Applied

Added a deferred closure immediately after the successful os.Open call that checks the named err return value. If Open returns an error on any subsequent validation step, the file is closed automatically. On the success path, err is nil so the file remains open for the caller to manage via File.Close(). This is a standard Go cleanup pattern.

Tests/Linters Ran

  • go vet ./fs/gguf/... — passed, no issues
  • gofmt -d fs/gguf/gguf.go — passed, no formatting differences
  • go test ./fs/gguf/... -v -count=1 — all 3 tests passed (TestValue, TestValues, TestRead)
  • golangci-lint and gofumpt are configured in .golangci.yaml but not available in the build environment; the code follows the same formatting and style conventions

Contribution Notes

  • Commit message follows the project's <package>: <short description> convention per CONTRIBUTING.md
  • No new dependencies added
  • Fix is minimal: 6 lines added, no unrelated changes
  • This is a security bug fix (file descriptor leak / DoS vector), which is listed as an ideal contribution type in CONTRIBUTING.md

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/14176 **Author:** [@kolega-ai-dev](https://github.com/kolega-ai-dev) **Created:** 2/9/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `v11-finding_26` --- ### 📝 Commits (1) - [`472ade3`](https://github.com/ollama/ollama/commit/472ade309cca13366a6808fdce3df28a9670051d) fs/gguf: close file on parsing errors in Open ### 📊 Changes **1 file changed** (+6 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `fs/gguf/gguf.go` (+6 -0) </details> ### 📄 Description *Vulnerability identified and fix provided by [Kolega.dev](https://kolega.dev/)* ## File Descriptor Leak on Parsing Errors ## Location `fs/gguf/gguf.go:47-91` ## Description The `Open()` function opens a file with `os.Open()` but fails to close it on multiple error return paths. If magic number validation, version check, or lazy loader initialization fails, the opened file descriptor is leaked. GGUF files can be uploaded by users via the `/api/create` endpoint. An attacker could repeatedly upload malformed GGUF files that fail validation checks, causing file descriptor exhaustion and denial of service. File descriptors are a finite system resource. ## Analysis Notes The `Open()` function at lines 47-91 opens a file with `os.Open()` at line 49 but fails to close it on error paths at lines 57, 61, 65, 69, 74, and 87. All callers (e.g. `server/images.go:79`) properly call `defer f.Close()` on success, but the error paths in `Open` itself were not covered. ## Fix Applied Added a deferred closure immediately after the successful `os.Open` call that checks the named `err` return value. If `Open` returns an error on any subsequent validation step, the file is closed automatically. On the success path, `err` is nil so the file remains open for the caller to manage via `File.Close()`. This is a standard Go cleanup pattern. ## Tests/Linters Ran - `go vet ./fs/gguf/...` — passed, no issues - `gofmt -d fs/gguf/gguf.go` — passed, no formatting differences - `go test ./fs/gguf/... -v -count=1` — all 3 tests passed (TestValue, TestValues, TestRead) - `golangci-lint` and `gofumpt` are configured in `.golangci.yaml` but not available in the build environment; the code follows the same formatting and style conventions ## Contribution Notes - Commit message follows the project's `<package>: <short description>` convention per `CONTRIBUTING.md` - No new dependencies added - Fix is minimal: 6 lines added, no unrelated changes - This is a security bug fix (file descriptor leak / DoS vector), which is listed as an ideal contribution type in `CONTRIBUTING.md` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-23 01:19:19 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#40425