instance-id: Ensure database is appropriately sized
Fail initialisation if we know that we may fail to satisfy valid
allocations.
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: Iff38b27fd324da57a1d81b6ad3c5951e369ce3b3
diff --git a/src/requester/instance-id.c b/src/requester/instance-id.c
index b746a96..7fe7c86 100644
--- a/src/requester/instance-id.c
+++ b/src/requester/instance-id.c
@@ -5,6 +5,7 @@
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
+#include <sys/stat.h>
#include <unistd.h>
#define BIT(i) (1UL << (i))
@@ -32,6 +33,8 @@
int pldm_instance_db_init(struct pldm_instance_db **ctx, const char *dbpath)
{
struct pldm_instance_db *l_ctx;
+ struct stat statbuf;
+ int rc;
/* Make sure the provided pointer was initialised to NULL. In the future
* if we stabilise the ABI and expose the struct definition the caller
@@ -41,6 +44,18 @@
return -EINVAL;
}
+ /* Ensure the underlying file is sized for properly managing allocations
+ */
+ rc = stat(dbpath, &statbuf);
+ if (rc < 0) {
+ return -EINVAL;
+ }
+
+ if (statbuf.st_size <
+ ((off_t)(PLDM_TID_MAX) * (off_t)(PLDM_INST_ID_MAX))) {
+ return -EINVAL;
+ }
+
l_ctx = calloc(1, sizeof(struct pldm_instance_db));
if (!l_ctx) {
return -ENOMEM;