Fix false ***buffer overflow*** detection 82/20882/1
authorJosé Bollo <jose.bollo@iot.bzh>
Tue, 2 Apr 2019 14:49:09 +0000 (16:49 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Tue, 2 Apr 2019 14:49:09 +0000 (16:49 +0200)
The compiling option __FORTIFY_SOURCE=2 introduced
a false ***buffer overflow*** detection when the
flexible array 'pattern' was initilized in globset.

The compiler is only complaining when the array is
in a struct that is in a struct like

 struct { ...; struct { ...; char name[1]; }}

To avoid these false detections, it is enougth
to ellipsese the dimension of the array. Seems
to be the now standard way of declaring flexible
arrays when it was before an extension. So now:

 struct { ...; struct { ...; char name[]; }}

works even when __FORTIFY_SOURCE=2.

Bug-AGL: SPEC-2292

Change-Id: I4b4a5df505a5357f92b9ab1657175911198ca582
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
12 files changed:
conf.d/packaging/rpm/agl-app-framework-binder.spec
src/afb-api-dbus.c
src/afb-api-ws.c
src/afb-apiset.c
src/afb-evt.c
src/afb-export.c
src/afb-hsrv.c
src/afb-stub-ws.c
src/afb-trace.c
src/globset.c
src/globset.h
src/locale-root.c

index 150fd9f..e41fd96 100644 (file)
@@ -57,7 +57,7 @@ This service is evolving permanently and is only designed as a helper for develo
 
 %build
 export PKG_CONFIG_PATH=%{_libdir}/pkgconfig
-%cmake  -DAGL_DEVEL=1 -DINCLUDE_MONITORING=ON -DCMAKE_C_FLAGS="-D_FORTIFY_SOURCE=1"
+%cmake  -DAGL_DEVEL=1 -DINCLUDE_MONITORING=ON"
 %__make %{?_smp_mflags}
 
 
index 562465e..1f254d1 100644 (file)
@@ -666,7 +666,7 @@ struct origin
        struct afb_cred *cred;
 
        /* the origin */
-       char name[1];
+       char name[];
 };
 
 /* get the credentials for the message */
@@ -710,7 +710,7 @@ static struct origin *afb_api_dbus_server_origin_get(struct api_dbus *api, const
        }
 
        /* not found, create it */
-       origin = malloc(strlen(sender) + sizeof *origin);
+       origin = malloc(strlen(sender) + 1 + sizeof *origin);
        if (origin == NULL)
                errno = ENOMEM;
        else {
index b9219d0..3d2445a 100644 (file)
@@ -43,7 +43,7 @@ struct api_ws_server
        struct afb_apiset *apiset;      /* the apiset for calling */
        struct fdev *fdev;              /* fdev handler */
        uint16_t offapi;                /* api name of the interface */
-       char uri[1];                    /* the uri of the server socket */
+       char uri[];                     /* the uri of the server socket */
 };
 
 /******************************************************************************/
@@ -206,7 +206,7 @@ int afb_api_ws_add_server(const char *uri, struct afb_apiset *declare_set, struc
        /* make the structure */
        lapi = strlen(api);
        extra = luri == (api - uri) + lapi ? 0 : lapi + 1;
-       apiws = malloc(sizeof * apiws + luri + extra);
+       apiws = malloc(sizeof * apiws + 1 + luri + extra);
        if (!apiws) {
                ERROR("out of memory");
                errno = ENOMEM;
index 468a364..16ded96 100644 (file)
@@ -73,7 +73,7 @@ struct api_alias
 {
        struct api_alias *next;
        struct api_desc *api;
-       char name[1];
+       char name[];
 };
 
 /**
@@ -83,7 +83,7 @@ struct api_class
 {
        struct api_class *next;
        struct api_array providers;
-       char name[1];
+       char name[];
 };
 
 /**
@@ -92,7 +92,7 @@ struct api_class
 struct api_depend
 {
        struct afb_apiset *set;
-       char name[1];
+       char name[];
 };
 
 /**
@@ -110,7 +110,7 @@ struct afb_apiset
        } onlack;                       /** not found handler */
        int timeout;                    /**< the timeout in second for the apiset */
        int refcount;                   /**< reference count for freeing resources */
-       char name[1];                   /**< name of the apiset */
+       char name[];                    /**< name of the apiset */
 };
 
 /**
@@ -215,7 +215,7 @@ static struct api_class *class_search(const char *name, int create)
        if (!create)
                return NULL;
 
-       c = calloc(1, strlen(name) + sizeof *c);
+       c = calloc(1, strlen(name) + 1 + sizeof *c);
        if (!c)
                errno = ENOMEM;
        else {
@@ -341,7 +341,7 @@ struct afb_apiset *afb_apiset_create(const char *name, int timeout)
 {
        struct afb_apiset *set;
 
-       set = calloc(1, (name ? strlen(name) : 0) + sizeof *set);
+       set = calloc(1, (name ? strlen(name) : 0) + 1 + sizeof *set);
        if (set) {
                set->timeout = timeout;
                set->refcount = 1;
@@ -545,7 +545,7 @@ int afb_apiset_add_alias(struct afb_apiset *set, const char *name, const char *a
        }
 
        /* allocates and init the struct */
-       ali = malloc(sizeof *ali + strlen(alias));
+       ali = malloc(sizeof *ali + strlen(alias) + 1);
        if (ali == NULL) {
                ERROR("out of memory");
                errno = ENOMEM;
@@ -1079,7 +1079,7 @@ int afb_apiset_require(struct afb_apiset *set, const char *name, const char *req
        if (!a)
                errno = ENOENT;
        else {
-               d = malloc(strlen(required) + sizeof *d);
+               d = malloc(strlen(required) + 1 + sizeof *d);
                if (!d)
                        errno = ENOMEM;
                else {
index 0467bef..a75cbbc 100644 (file)
@@ -86,7 +86,7 @@ struct afb_evtid {
        int id;
 
        /* fullname of the event */
-       char fullname[1];
+       char fullname[];
 };
 
 /*
@@ -296,7 +296,7 @@ struct afb_evtid *afb_evt_evtid_create(const char *fullname)
 
        /* allocates the event */
        len = strlen(fullname);
-       evtid = malloc(len + sizeof * evtid);
+       evtid = malloc(len + 1 + sizeof * evtid);
        if (evtid == NULL)
                goto error;
 
index 8ebe8e0..b46e6a0 100644 (file)
@@ -170,7 +170,7 @@ struct afb_export
        } export;
 
        /* initial name */
-       char name[1];
+       char name[];
 };
 
 /*****************************************************************************/
@@ -1372,7 +1372,7 @@ static struct afb_export *create(
                        return NULL;
        }
        lenapi = strlen(apiname);
-       export = calloc(1, sizeof *export + lenapi + (path == apiname || !path ? 0 : strlen(path)));
+       export = calloc(1, sizeof *export + 1 + lenapi + (path == apiname || !path ? 0 : strlen(path)));
        if (!export)
                errno = ENOMEM;
        else {
index ed0adee..3f11047 100644 (file)
@@ -56,7 +56,7 @@ struct hsrv_itf {
        struct hsrv_itf *next;
        struct afb_hsrv *hsrv;
        struct fdev *fdev;
-       char uri[1];
+       char uri[];
 };
 
 struct hsrv_handler {
@@ -562,7 +562,7 @@ int afb_hsrv_add_interface(struct afb_hsrv *hsrv, const char *uri)
 {
        struct hsrv_itf *itf;
 
-       itf = malloc(sizeof *itf + strlen(uri));
+       itf = malloc(sizeof *itf + 1 + strlen(uri));
        if (itf == NULL)
                return -1;
 
index b362c12..3c28871 100644 (file)
@@ -145,7 +145,7 @@ struct afb_stub_ws
        uint8_t is_client;
 
        /* the api name */
-       char apiname[1];
+       char apiname[];
 };
 
 static struct afb_proto_ws *afb_stub_ws_create_proto(struct afb_stub_ws *stubws, struct fdev *fdev, uint8_t server);
@@ -673,7 +673,7 @@ static struct afb_stub_ws *afb_stub_ws_create(struct fdev *fdev, const char *api
 {
        struct afb_stub_ws *stubws;
 
-       stubws = calloc(1, sizeof *stubws + strlen(apiname));
+       stubws = calloc(1, sizeof *stubws + 1 + strlen(apiname));
        if (stubws == NULL)
                errno = ENOMEM;
        else {
index 802015f..0de78da 100644 (file)
@@ -67,7 +67,7 @@
 /* struct for tags */
 struct tag {
        struct tag *next;       /* link to the next */
-       char tag[1];            /* name of the tag */
+       char tag[];             /* name of the tag */
 };
 
 /* struct for events */
@@ -1073,7 +1073,7 @@ static struct tag *trace_get_tag(struct afb_trace *trace, const char *name, int
 
        if (!tag && alloc) {
                /* creation if needed */
-               tag = malloc(sizeof * tag + strlen(name));
+               tag = malloc(sizeof * tag + 1 + strlen(name));
                if (tag) {
                        strcpy(tag->tag, name);
                        tag->next = trace->tags;
index 2bad449..5e414dd 100644 (file)
@@ -323,7 +323,7 @@ int globset_add(
        }
 
        /* not found, create it */
-       ph = malloc(len + sizeof *ph);
+       ph = malloc(1 + len + sizeof *ph);
        if (!ph)
                return -1;
 
index 58cbd3d..85fdd19 100644 (file)
@@ -26,7 +26,7 @@ struct globset_handler
        void *closure;
 
        /* the pattern */
-       char pattern[1];
+       char pattern[];
 };
 
 struct globset;
index fa620fe..4d141b8 100644 (file)
@@ -47,7 +47,7 @@ static const char locales[] = "locales/";
 struct locale_folder {
        struct locale_folder *parent;
        size_t length;
-       char name[1];
+       char name[];
 };
 
 struct locale_container {
@@ -67,7 +67,7 @@ struct locale_search {
        struct locale_root *root;
        struct locale_search_node *head;
        int refcount;
-       char definition[1];
+       char definition[];
 };
 
 struct locale_root {
@@ -102,7 +102,7 @@ static int add_folder(struct locale_container *container, const char *name)
        if (folders != NULL) {
                container->folders = folders;
                length = strlen(name);
-               folders[count] = malloc(sizeof **folders + length);
+               folders[count] = malloc(sizeof **folders + 1 + length);
                if (folders[count] != NULL) {
                        folders[count]->parent = NULL;
                        folders[count]->length = length;
@@ -362,7 +362,7 @@ static struct locale_search *create_search(struct locale_root *root, const char
        struct locale_search_node *node;
 
        /* allocate the structure */
-       search = malloc(sizeof *search + length);
+       search = malloc(sizeof *search + 1 + length);
        if (search == NULL) {
                errno = ENOMEM;
        } else {