filter_kubernetes: strict validation of file descriptor (CID 184257)
@@ -174,18 +174,22 @@ static int get_api_server_info(struct flb_kube *ctx,
* e.g. debugging new parsers.
*/
if (ctx->meta_preload_cache_dir && namespace && podname) {
- int fd;
+ int fd = -1;
char *payload = NULL;
size_t payload_size = 0;
struct stat sb;
- ret = snprintf(uri, sizeof(uri) - 1, "%s/%s_%s.meta", ctx->meta_preload_cache_dir, namespace, podname);
+ ret = snprintf(uri, sizeof(uri) - 1, "%s/%s_%s.meta",
+ ctx->meta_preload_cache_dir, namespace, podname);
if (ret > 0) {
fd = open(uri, O_RDONLY, 0);
- if (fd > 0) {
+ if (fd != -1) {
if (fstat(fd, &sb) == 0) {
payload = flb_malloc(sb.st_size);
- if (payload) {
+ if (!payload) {
+ flb_errno();
+ }
+ else {
ret = read(fd, payload, sb.st_size);
if (ret == sb.st_size) {
payload_size = ret;
@@ -195,6 +199,7 @@ static int get_api_server_info(struct flb_kube *ctx,
close(fd);
}
}
+
if (payload_size) {
packed = flb_pack_json(payload, payload_size,
&buf, &size, &root_type);
