[GH-ISSUE #3549] modelfile parser panics when supplied with crafted input #2190

Closed
opened 2026-04-12 12:26:20 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @sepehrdaddev on GitHub (Apr 9, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/3549

Originally assigned to: @royjhan on GitHub.

What is the issue?

One of my fuzzers found that when the user supplies crafted inputs to the modelfile parser, the application would crash (panics),
this is unexpected as the application should exit with a parsing error rather than a panic.

What did you expect to see?

I expected the application to exit with a parsing error while it just paniced.

Steps to reproduce

I found multiple places where the parser mishandles the user input, one of them is when the FROM statement in which when a crafted input is supplied using the following command: $ ollama create crash -f ./from-crash.txt
it would panic with the following stack trace:

2024/04/09 07:48:43 parser.go:73: WARN Unknown command: (((((((((((��(((((((((((((�
2024/04/09 07:48:43 parser.go:73: WARN Unknown command: ������������������������������������������������������������������������������������������������������������������������������������������������'�
2024/04/09 07:48:43 parser.go:73: WARN Unknown command: (((((((((((((((((
2024/04/09 07:48:43 parser.go:73: WARN Unknown command: 
2024/04/09 07:48:43 parser.go:73: WARN Unknown command: (((+
2024/04/09 07:48:43 parser.go:73: WARN Unknown command: ������������������������������������������������'�

panic: runtime error: index out of range [1] with length 1

goroutine 1 [running]:
github.com/ollama/ollama/parser.Parse({0x1186d9a0?, 0xc0004290b0?})
	github.com/ollama/ollama/parser/parser.go:41 +0xb68
github.com/ollama/ollama/cmd.CreateHandler(0xc000406c08, {0xc000428ba0, 0x1, 0x1055b75?})
	github.com/ollama/ollama/cmd/cmd.go:63 +0x1f2
github.com/spf13/cobra.(*Command).execute(0xc000406c08, {0xc000428b40, 0x3, 0x3})
	github.com/spf13/cobra@v1.7.0/command.go:940 +0x882
github.com/spf13/cobra.(*Command).ExecuteC(0xc000406908)
	github.com/spf13/cobra@v1.7.0/command.go:1068 +0x3a5
github.com/spf13/cobra.(*Command).Execute(...)
	github.com/spf13/cobra@v1.7.0/command.go:992
github.com/spf13/cobra.(*Command).ExecuteContext(...)
	github.com/spf13/cobra@v1.7.0/command.go:985
main.main()
	github.com/ollama/ollama/main.go:11 +0x4d

this indicates that parser.go#L41 is the one causing it.

the same happens but for the PROMPT statement, when using the following command: $ ollama create crash -f ./prompt-crash.txt
it would panic with the following stack trace:

2024/04/09 07:51:36 parser.go:73: WARN Unknown command: 1(
panic: runtime error: index out of range [1] with length 1

goroutine 1 [running]:
github.com/ollama/ollama/parser.Parse({0x1186d9a0?, 0xc0004ba210?})
	github.com/ollama/ollama/parser/parser.go:49 +0x9d4
github.com/ollama/ollama/cmd.CreateHandler(0xc000484c08, {0xc0000aca80, 0x1, 0x1055b75?})
	github.com/ollama/ollama/cmd/cmd.go:63 +0x1f2
github.com/spf13/cobra.(*Command).execute(0xc000484c08, {0xc0000aca20, 0x3, 0x3})
	github.com/spf13/cobra@v1.7.0/command.go:940 +0x882
github.com/spf13/cobra.(*Command).ExecuteC(0xc000484908)
	github.com/spf13/cobra@v1.7.0/command.go:1068 +0x3a5
github.com/spf13/cobra.(*Command).Execute(...)
	github.com/spf13/cobra@v1.7.0/command.go:992
github.com/spf13/cobra.(*Command).ExecuteContext(...)
	github.com/spf13/cobra@v1.7.0/command.go:985
main.main()
	github.com/ollama/ollama/main.go:11 +0x4d

this indicates that parser.go#L49 is the one causing it.

from-crash.txt
prompt-crash.txt

Are there any recent changes that introduced the issue?

n/a

OS

Linux

Architecture

amd64

Platform

No response

Ollama version

0.1.31

GPU

AMD

GPU info

No response

CPU

AMD

Other software

No response

Originally created by @sepehrdaddev on GitHub (Apr 9, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/3549 Originally assigned to: @royjhan on GitHub. ### What is the issue? One of my fuzzers found that when the user supplies crafted inputs to the modelfile parser, the application would crash (panics), this is unexpected as the application should exit with a parsing error rather than a panic. ### What did you expect to see? I expected the application to exit with a parsing error while it just paniced. ### Steps to reproduce I found multiple places where the parser mishandles the user input, one of them is when the `FROM` statement in which when a crafted input is supplied using the following command: `$ ollama create crash -f ./from-crash.txt` it would panic with the following stack trace: ``` 2024/04/09 07:48:43 parser.go:73: WARN Unknown command: (((((((((((��(((((((((((((� 2024/04/09 07:48:43 parser.go:73: WARN Unknown command: ������������������������������������������������������������������������������������������������������������������������������������������������'� 2024/04/09 07:48:43 parser.go:73: WARN Unknown command: ((((((((((((((((( 2024/04/09 07:48:43 parser.go:73: WARN Unknown command: 2024/04/09 07:48:43 parser.go:73: WARN Unknown command: (((+ 2024/04/09 07:48:43 parser.go:73: WARN Unknown command: ������������������������������������������������'� panic: runtime error: index out of range [1] with length 1 goroutine 1 [running]: github.com/ollama/ollama/parser.Parse({0x1186d9a0?, 0xc0004290b0?}) github.com/ollama/ollama/parser/parser.go:41 +0xb68 github.com/ollama/ollama/cmd.CreateHandler(0xc000406c08, {0xc000428ba0, 0x1, 0x1055b75?}) github.com/ollama/ollama/cmd/cmd.go:63 +0x1f2 github.com/spf13/cobra.(*Command).execute(0xc000406c08, {0xc000428b40, 0x3, 0x3}) github.com/spf13/cobra@v1.7.0/command.go:940 +0x882 github.com/spf13/cobra.(*Command).ExecuteC(0xc000406908) github.com/spf13/cobra@v1.7.0/command.go:1068 +0x3a5 github.com/spf13/cobra.(*Command).Execute(...) github.com/spf13/cobra@v1.7.0/command.go:992 github.com/spf13/cobra.(*Command).ExecuteContext(...) github.com/spf13/cobra@v1.7.0/command.go:985 main.main() github.com/ollama/ollama/main.go:11 +0x4d ``` this indicates that [parser.go#L41](https://github.com/ollama/ollama/blob/main/parser/parser.go#L41) is the one causing it. the same happens but for the `PROMPT` statement, when using the following command: `$ ollama create crash -f ./prompt-crash.txt` it would panic with the following stack trace: ``` 2024/04/09 07:51:36 parser.go:73: WARN Unknown command: 1( panic: runtime error: index out of range [1] with length 1 goroutine 1 [running]: github.com/ollama/ollama/parser.Parse({0x1186d9a0?, 0xc0004ba210?}) github.com/ollama/ollama/parser/parser.go:49 +0x9d4 github.com/ollama/ollama/cmd.CreateHandler(0xc000484c08, {0xc0000aca80, 0x1, 0x1055b75?}) github.com/ollama/ollama/cmd/cmd.go:63 +0x1f2 github.com/spf13/cobra.(*Command).execute(0xc000484c08, {0xc0000aca20, 0x3, 0x3}) github.com/spf13/cobra@v1.7.0/command.go:940 +0x882 github.com/spf13/cobra.(*Command).ExecuteC(0xc000484908) github.com/spf13/cobra@v1.7.0/command.go:1068 +0x3a5 github.com/spf13/cobra.(*Command).Execute(...) github.com/spf13/cobra@v1.7.0/command.go:992 github.com/spf13/cobra.(*Command).ExecuteContext(...) github.com/spf13/cobra@v1.7.0/command.go:985 main.main() github.com/ollama/ollama/main.go:11 +0x4d ``` this indicates that [parser.go#L49](https://github.com/ollama/ollama/blob/main/parser/parser.go#L49) is the one causing it. [from-crash.txt](https://github.com/ollama/ollama/files/14913955/from-crash.txt) [prompt-crash.txt](https://github.com/ollama/ollama/files/14913956/prompt-crash.txt) ### Are there any recent changes that introduced the issue? n/a ### OS Linux ### Architecture amd64 ### Platform _No response_ ### Ollama version 0.1.31 ### GPU AMD ### GPU info _No response_ ### CPU AMD ### Other software _No response_
GiteaMirror added the bug label 2026-04-12 12:26:20 -05:00
Author
Owner

@sepehrdaddev commented on GitHub (Apr 9, 2024):

after further investigation I noticed that all the verbs will crash if they are used in the modelfile without a space, here is an example:

$ for payload in "FROM" "ADAPTER" "LICENSE" "TEMPLATE" "SYSTEM" "PROMPT" "PARAMETER" "MESSAGE"; do echo $payload > payload && ollama create crash -f ./payload && rm -f payload; done

this is because the fields[1] is used improperly in the switch statement

<!-- gh-comment-id:2044351600 --> @sepehrdaddev commented on GitHub (Apr 9, 2024): after further investigation I noticed that all the verbs will crash if they are used in the modelfile without a space, here is an example: ```sh $ for payload in "FROM" "ADAPTER" "LICENSE" "TEMPLATE" "SYSTEM" "PROMPT" "PARAMETER" "MESSAGE"; do echo $payload > payload && ollama create crash -f ./payload && rm -f payload; done ``` this is because the `fields[1]` is used improperly in the switch statement
Author
Owner

@royjhan commented on GitHub (May 29, 2024):

Thanks for reporting the issue, do you mind trying again to ensure this has been resolved? We're no longer able to reproduce the issue on our end.

<!-- gh-comment-id:2138365666 --> @royjhan commented on GitHub (May 29, 2024): Thanks for reporting the issue, do you mind trying again to ensure this has been resolved? We're no longer able to reproduce the issue on our end.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#2190