Possible memory leak of the config map array and its C strings in `output.setConfigMap`

Aperta Adatta ai principianti
#85 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
84/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c, go
Ambito
backend

Direzione di ricerca

Inizia in output/output.go leggendo setConfigMap e FLBPluginUnregister, quindi esamina i riferimenti a config_map con il comando grep indicato. Verifica che la pulizia durante la deregistrazione copra ogni stringa config-map allocata e l'array, e che il puntatore venga azzerato senza modificare il problema separato di input/input.go.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Possible memory leak of the config map array and its C strings in output.setConfigMap

setConfigMap allocates the config map array plus three C strings per entry, but
FLBPluginUnregister only frees p.name and p.description. The array and all
3 * len(cmap) strings are leaked.

output/output.go:139

func setConfigMap(p *FLBPluginProxyDef, cmap []ConfigMap) {
	if len(cmap) == 0 {
		return
	}

	cfg := (*C.struct_flb_config_map)(C.calloc(C.size_t(len(cmap)+1), C.sizeof_struct_flb_config_map))
	entries := (*[1 << 28]C.struct_flb_config_map)(unsafe.Pointer(cfg))[:len(cmap):len(cmap)]
	for i, m := range cmap {
		entries[i]._type = C.int(m.Type)
		entries[i].name = C.CString(m.Name)
		entries[i].flags = C.int(m.Flags)
		entries[i].def_value = C.CString(m.DefValue)
		entries[i].desc = C.CString(m.Desc)
	}

	p.config_map = cfg
}

output/output.go:158, the function that is supposed to clean this up:

// Release resources allocated by the plugin initialization
func FLBPluginUnregister(def unsafe.Pointer) {
	p := (*FLBPluginProxyDef)(def)
	C.free(unsafe.Pointer(p.name))
	C.free(unsafe.Pointer(p.description))
}

Fluent Bit reads the config map during init and does not take ownership of the
allocations, which is why p.name and p.description are freed here explicitly.
grep -rn config_map output/ shows only the three lines above, so nothing else releases
them.

This is bounded by the number of plugin registrations rather than by traffic, so it is
one leak per plugin instance rather than growth under load.

Fix: free the entries and the array in FLBPluginUnregister. The array is
NUL-terminated by the extra calloced entry, so it can be walked until name is nil:

	if p.config_map != nil {
		e := (*[1 << 28]C.struct_flb_config_map)(unsafe.Pointer(p.config_map))
		for i := 0; e[i].name != nil; i++ {
			C.free(unsafe.Pointer(e[i].name))
			C.free(unsafe.Pointer(e[i].def_value))
			C.free(unsafe.Pointer(e[i].desc))
		}
		C.free(unsafe.Pointer(p.config_map))
		p.config_map = nil
	}

input/input.go:112 has the same code and the same gap; separate issue for that one.

If you could credit me as a reporter for my contributions to security advisory I will be thankful.

Lingua principale
Go
Stelle
202
Fork
63
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di fluent/fluent-bit-go

Tutte le issue di fluent/fluent-bit-go

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.