Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map removal fix #340

Closed
wants to merge 9 commits into from
11 changes: 10 additions & 1 deletion kf/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,10 +1104,19 @@ func (b *BPF) RemoveMapFiles(ifaceName string) error {
} else {
mapFilename = filepath.Join(b.hostConfig.BpfMapDefaultPath, ifaceName, k)
}
if !strings.HasPrefix(mapFilename, b.hostConfig.BpfMapDefaultPath) {
return fmt.Errorf("malicious mapFilename path")
}
if err := v.Unpin(); err != nil {
return fmt.Errorf("BPF program %s prog type %s ifacename %s map %s:failed to pin the map err - %#v",
return fmt.Errorf("BPF program %s prog type %s ifacename %s map %s:failed to unpin the map: %v",
b.Program.Name, b.Program.ProgType, ifaceName, mapFilename, err)
}
if fileExists(mapFilename) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a BPF map is shared by multiple programs, and one program unpins the map while another program is still accessing it, it can lead to unpredictable behavior. To avoid this, you can check if the reference count of the map is less than 1 using v.GetRefCount(), and then proceed to delete the file.

Copy link
Contributor Author

@Atul-source Atul-source Mar 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is v.GetRefCount() a library function ?

log.Warn().Msgf("Removing the Map file, as unpinning not able to remove map file: %v", mapFilename)
if err := os.RemoveAll(mapFilename); err != nil {
return fmt.Errorf("removal of %v failed", mapFilename)
}
}
}
return nil
}
Expand Down
Loading