Commit 8eb8b2b
authored
[restapi] Fix nil pointer dereference in monitor_certs when cert file is missing (#185)
#### Why I did it
`monitor_certs()` in `go-server-server/main.go` calls `os.Lstat()` on each cert file path but silently discards the error return value with `_`. When a cert file is missing or its path is invalid, `os.Lstat()` returns `(nil, error)`. Calling `.ModTime()` on the nil `FileInfo` immediately causes a **nil pointer dereference (SIGSEGV)** that crashes the entire restapi process.
Observed crash in SONiC nightly CI (Arista 4600C, image 20251110.22):
```
goroutine 1 [running]:
main.monitor_certs(0xc000056060, 0xc000012100)
go-server-server/main.go:122 +0x...
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation addr=0x20 pc=...]
```
After the crash, `supervisord` restarted restapi and the proc-exit-listener emitted ERR alerts every 60 seconds, causing `system_health/test_system_status` to fail.
##### Work item tracking
- Microsoft ADO: [37693847](https://msazure.visualstudio.com/One/_workitems/edit/37693847)
#### How I did it
Added explicit nil/error checks after every `os.Lstat()` call in `monitor_certs` (6 total):
- **Startup section (3 calls):** use `log.Fatalf` so the process fails immediately with a descriptive error message instead of panicking with an opaque SIGSEGV.
- **Monitor loop (3 calls):** use `log.Printf` + `time.Sleep` + `continue` so a transiently missing cert file is retried on the next cycle (every 3600 s) without crashing the server.
#### How to verify it
1. Start restapi with `--https` flag and a cert path that does not exist on disk.
2. **Before fix:** process crashes with `panic: runtime error: invalid memory address or nil pointer dereference`.
3. **After fix:** process exits cleanly via `log.Fatalf` with `"error: couldn't stat client cert file: ..."`.
#### Which release branch to backport (provide reason below if selected)
- [ ] 202305
- [ ] 202311
- [ ] 202405
- [ ] 202411
- [ ] 202505
- [x] 202511 — same crash observed on image 20251110.22 (202511 branch)
#### Tested branch (Please provide the tested image version)
- [x] 20251110.22 (202511) — crash confirmed in nightly CI job [69e79fb88e43924279229609](https://elastictest.org/scheduler/testplan/69e79fb88e43924279229609)
#### Description for the changelog
Fix nil pointer dereference crash in `monitor_certs` when a TLS cert file path is invalid or missing; restapi now exits with a descriptive error at startup instead of panicking silently.
Signed-off-by: Sonic Build Admin <sonicbld@microsoft.com>1 parent df38186 commit 8eb8b2b
1 file changed
Lines changed: 30 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
106 | 109 | | |
107 | 110 | | |
108 | 111 | | |
109 | | - | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
110 | 116 | | |
111 | 117 | | |
112 | 118 | | |
113 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
114 | 123 | | |
115 | 124 | | |
116 | 125 | | |
117 | 126 | | |
118 | 127 | | |
119 | 128 | | |
120 | 129 | | |
121 | | - | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
122 | 136 | | |
123 | 137 | | |
124 | 138 | | |
| |||
127 | 141 | | |
128 | 142 | | |
129 | 143 | | |
130 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
131 | 150 | | |
132 | 151 | | |
133 | 152 | | |
| |||
136 | 155 | | |
137 | 156 | | |
138 | 157 | | |
139 | | - | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
140 | 164 | | |
141 | 165 | | |
142 | 166 | | |
| |||
0 commit comments