>From 4030e7c0a3eca06d096f44b6ae35ffc750578a9a Mon Sep 17 00:00:00 2001
From: Michal Zidek <mzidek@redhat.com>
Date: Wed, 28 Aug 2013 12:46:58 +0200
Subject: [PATCH] responder: Use SAFEALIGN macros where appropriate.

https://fedorahosted.org/sssd/ticket/1359
---
 src/responder/autofs/autofssrv_cmd.c  |  8 +++-
 src/responder/common/responder_cmd.c  | 18 +++++---
 src/responder/nss/nsssrv_cmd.c        | 77 +++++++++++++++++++----------------
 src/responder/nss/nsssrv_mmap_cache.c |  2 +-
 src/responder/nss/nsssrv_netgroup.c   | 18 +++++---
 src/responder/nss/nsssrv_services.c   |  9 ++--
 6 files changed, 81 insertions(+), 51 deletions(-)

diff --git a/src/responder/autofs/autofssrv_cmd.c b/src/responder/autofs/autofssrv_cmd.c
index 32ea510..a56003c 100644
--- a/src/responder/autofs/autofssrv_cmd.c
+++ b/src/responder/autofs/autofssrv_cmd.c
@@ -326,8 +326,12 @@ static void sss_autofs_cmd_setautomntent_done(struct tevent_req *req)
             }
 
             sss_packet_get_body(packet, &body, &blen);
-            ((uint32_t *)body)[0] = 1; /* Got some results */
-            ((uint32_t *)body)[1] = 0; /* reserved */
+
+            /* Got some results */
+            SAFEALIGN_SETMEM_UINT32(body, 1, NULL);
+
+            /* Reserved padding */
+            SAFEALIGN_SETMEM_UINT32(body + sizeof(uint32_t), 0, NULL);
         }
 
         sss_cmd_done(cmdctx->cctx, NULL);
diff --git a/src/responder/common/responder_cmd.c b/src/responder/common/responder_cmd.c
index 3a3fca9..b7483d6 100644
--- a/src/responder/common/responder_cmd.c
+++ b/src/responder/common/responder_cmd.c
@@ -51,8 +51,12 @@ int sss_cmd_empty_packet(struct sss_packet *packet)
     if (ret != EOK) return ret;
 
     sss_packet_get_body(packet, &body, &blen);
-    ((uint32_t *)body)[0] = 0; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
+
+    /* num results */
+    SAFEALIGN_SETMEM_UINT32(body, 0, NULL);
+
+    /* reserved */
+    SAFEALIGN_SETMEM_UINT32(body + sizeof(uint32_t), 0, NULL);
 
     return EOK;
 }
@@ -97,6 +101,7 @@ int sss_cmd_get_version(struct cli_ctx *cctx)
     size_t blen;
     int ret;
     uint32_t client_version;
+    uint32_t protocol_version;
     int i;
     static struct cli_protocol_version *cli_protocol_version = NULL;
 
@@ -133,9 +138,12 @@ int sss_cmd_get_version(struct cli_ctx *cctx)
         return ret;
     }
     sss_packet_get_body(cctx->creq->out, &body, &blen);
-    ((uint32_t *)body)[0] = cctx->cli_protocol_version!=NULL ?
-                                cctx->cli_protocol_version->version : 0;
-    DEBUG(5, ("Offered version [%d].\n", ((uint32_t *)body)[0]));
+
+    protocol_version = (cctx->cli_protocol_version != NULL)
+                       ? cctx->cli_protocol_version->version : 0;
+
+    SAFEALIGN_COPY_UINT32(body, &protocol_version, NULL);
+    DEBUG(SSSDBG_FUNC_DATA, ("Offered version [%d].\n", protocol_version));
 
     sss_cmd_done(cctx, NULL);
     return EOK;
diff --git a/src/responder/nss/nsssrv_cmd.c b/src/responder/nss/nsssrv_cmd.c
index 6a1e6a0..e7cd9a0 100644
--- a/src/responder/nss/nsssrv_cmd.c
+++ b/src/responder/nss/nsssrv_cmd.c
@@ -462,8 +462,8 @@ done:
     if (!packet_initialized) return ENOENT;
 
     sss_packet_get_body(packet, &body, &blen);
-    ((uint32_t *)body)[0] = num; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
+    SAFEALIGN_COPY_UINT32(body, &num, NULL); /* num results */
+    SAFEALIGN_SETMEM_UINT32(body + sizeof(uint32_t), 0, NULL); /* reserved */
 
     return EOK;
 }
@@ -991,6 +991,7 @@ static int nss_check_name_of_well_known_sid(struct nss_cmd_ctx *cmdctx,
     size_t blen;
     struct cli_ctx *cctx;
     struct nss_ctx *nss_ctx;
+    size_t pctr = 0;
 
     nss_ctx = talloc_get_type(cmdctx->cctx->rctx->pvt_ctx, struct nss_ctx);
     ret = sss_parse_name(cmdctx, nss_ctx->global_names, full_name,
@@ -1020,10 +1021,10 @@ static int nss_check_name_of_well_known_sid(struct nss_cmd_ctx *cmdctx,
     }
 
     sss_packet_get_body(cctx->creq->out, &body, &blen);
-    ((uint32_t *)body)[0] = 1; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
-    ((uint32_t *)body)[2] = (uint32_t) SSS_ID_TYPE_GID;
-    memcpy(&body[3*sizeof(uint32_t)], sid.str, sid.len);
+    SAFEALIGN_SETMEM_UINT32(body, 1, &pctr);  /* num results */
+    SAFEALIGN_SETMEM_UINT32(body + pctr, 0, &pctr); /* reserved */
+    SAFEALIGN_SETMEM_UINT32(body + pctr, SSS_ID_TYPE_GID, &pctr);
+    memcpy(&body[pctr], sid.str, sid.len);
 
     sss_packet_set_error(cctx->creq->out, EOK);
     sss_cmd_done(cctx, cmdctx);
@@ -1456,7 +1457,7 @@ static int nss_cmd_getbyid(enum sss_cli_command cmd, struct cli_ctx *cctx)
         ret = EINVAL;
         goto done;
     }
-    cmdctx->id = *((uint32_t *)body);
+    SAFEALIGN_COPY_UINT32(&cmdctx->id, body, NULL);
 
     DEBUG(SSSDBG_TRACE_FUNC, ("Running command [%d] with id [%d].\n",
                               dctx->cmdctx->cmd, cmdctx->id));
@@ -2035,7 +2036,7 @@ static int nss_cmd_getpwent_immediate(struct nss_cmd_ctx *cmdctx)
     if (blen != sizeof(uint32_t)) {
         return EINVAL;
     }
-    num = *((uint32_t *)body);
+    SAFEALIGN_COPY_UINT32(&num, body, NULL);
 
     /* create response packet */
     ret = sss_packet_new(cctx->creq, 0,
@@ -2581,8 +2582,8 @@ done:
         return ENOENT;
     }
 
-    ((uint32_t *)body)[0] = num; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
+    SAFEALIGN_COPY_UINT32(body, &num, NULL); /* num results */
+    SAFEALIGN_SETMEM_UINT32(body + sizeof(uint32_t), 0, NULL); /* reserved */
 
     return EOK;
 }
@@ -3299,7 +3300,7 @@ static int nss_cmd_getgrent_immediate(struct nss_cmd_ctx *cmdctx)
     if (blen != sizeof(uint32_t)) {
         return EINVAL;
     }
-    num = *((uint32_t *)body);
+    SAFEALIGN_COPY_UINT32(&num, body, NULL);
 
     /* create response packet */
     ret = sss_packet_new(cctx->creq, 0,
@@ -3557,8 +3558,11 @@ static int fill_initgr(struct sss_packet *packet, struct ldb_result *res)
         }
     }
 
+    /* 0-3: 32bit unsigned number of results
+     * 4-7: 32bit unsigned (reserved/padding) */
+    bindex = 2 * sizeof(uint32_t);
+
     /* skip first entry, it's the user entry */
-    bindex = 0;
     for (i = 0; i < num; i++) {
         gid = ldb_msg_find_attr_as_uint64(res->msgs[i + 1], SYSDB_GIDNUM, 0);
         posix = ldb_msg_find_attr_as_string(res->msgs[i + 1], SYSDB_POSIX, NULL);
@@ -3571,8 +3575,7 @@ static int fill_initgr(struct sss_packet *packet, struct ldb_result *res)
                 return EFAULT;
             }
         }
-        ((uint32_t *)body)[2 + bindex] = gid;
-        bindex++;
+        SAFEALIGN_COPY_UINT32(body + bindex, &gid, &bindex);
 
         /* do not add the GID of the original primary group is the user is
          * already and explicit member of the group. */
@@ -3582,14 +3585,13 @@ static int fill_initgr(struct sss_packet *packet, struct ldb_result *res)
     }
 
     if (orig_primary_gid != 0) {
-        ((uint32_t *)body)[2 + bindex] = orig_primary_gid;
-        bindex++;
+        SAFEALIGN_COPY_UINT32(body + bindex, &orig_primary_gid, &bindex);
         num++;
     }
 
-    ((uint32_t *)body)[0] = num-skipped; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
-    blen = (2 + bindex) * sizeof(uint32_t);
+    SAFEALIGN_SETMEM_UINT32(body, num - skipped, NULL); /* num results */
+    SAFEALIGN_SETMEM_UINT32(body + sizeof(uint32_t), 0, NULL); /* reserved */
+    blen = bindex;
     ret = sss_packet_set_size(packet, blen);
     if (ret != EOK) {
         DEBUG(SSSDBG_OP_FAILURE,
@@ -4142,6 +4144,7 @@ static errno_t fill_sid(struct sss_packet *packet,
     struct sized_string sid;
     uint8_t *body;
     size_t blen;
+    size_t pctr = 0;
 
     sid_str = ldb_msg_find_attr_as_string(msg, SYSDB_SID_STR, NULL);
     if (sid_str == NULL) {
@@ -4158,10 +4161,10 @@ static errno_t fill_sid(struct sss_packet *packet,
     }
 
     sss_packet_get_body(packet, &body, &blen);
-    ((uint32_t *)body)[0] = 1; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
-    ((uint32_t *)body)[2] = id_type;
-    memcpy(&body[3*sizeof(uint32_t)], sid.str, sid.len);
+    SAFEALIGN_SETMEM_UINT32(body, 1, &pctr); /* Num results */
+    SAFEALIGN_SETMEM_UINT32(body + pctr, 0, &pctr); /* reserved */
+    SAFEALIGN_COPY_UINT32(body + pctr, &id_type, &pctr);
+    memcpy(&body[pctr], sid.str, sid.len);
 
     return EOK;
 }
@@ -4180,6 +4183,7 @@ static errno_t fill_name(struct sss_packet *packet,
     bool add_domain = (!IS_SUBDOMAIN(dom) && dom->fqnames);
     uint8_t *body;
     size_t blen;
+    size_t pctr = 0;
 
     orig_name = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
     if (orig_name == NULL) {
@@ -4219,10 +4223,11 @@ static errno_t fill_name(struct sss_packet *packet,
     }
 
     sss_packet_get_body(packet, &body, &blen);
-    ((uint32_t *)body)[0] = 1; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
-    ((uint32_t *)body)[2] = id_type;
-    memcpy(&body[3*sizeof(uint32_t)], name.str, name.len);
+    SAFEALIGN_SETMEM_UINT32(body, 1, &pctr); /* Num results */
+    SAFEALIGN_SETMEM_UINT32(body + pctr, 0, &pctr); /* reserved */
+    SAFEALIGN_COPY_UINT32(body + pctr, &id_type, &pctr);
+    memcpy(&body[pctr], name.str, name.len);
+
 
     ret = EOK;
 
@@ -4239,6 +4244,7 @@ static errno_t fill_id(struct sss_packet *packet,
     int ret;
     uint8_t *body;
     size_t blen;
+    size_t pctr = 0;
     uint64_t id;
 
     if (id_type == SSS_ID_TYPE_GID) {
@@ -4259,10 +4265,10 @@ static errno_t fill_id(struct sss_packet *packet,
     }
 
     sss_packet_get_body(packet, &body, &blen);
-    ((uint32_t *)body)[0] = 1; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
-    ((uint32_t *)body)[2] = (uint32_t) id_type;
-    ((uint32_t *)body)[3] = (uint32_t) id;
+    SAFEALIGN_SETMEM_UINT32(body, 1, &pctr); /* Num results */
+    SAFEALIGN_SETMEM_UINT32(body + pctr, 0, &pctr); /* reserved */
+    SAFEALIGN_COPY_UINT32(body + pctr, &id_type, &pctr);
+    SAFEALIGN_COPY_UINT32(body + pctr, &id, &pctr);
 
     return EOK;
 }
@@ -4331,6 +4337,7 @@ static int nss_check_well_known_sid(struct nss_cmd_ctx *cmdctx)
     size_t blen;
     struct cli_ctx *cctx;
     struct nss_ctx *nss_ctx;
+    size_t pctr = 0;
 
     ret = well_known_sid_to_name(cmdctx->secid, &wk_dom_name, &wk_name);
     if (ret != EOK) {
@@ -4368,10 +4375,10 @@ static int nss_check_well_known_sid(struct nss_cmd_ctx *cmdctx)
     }
 
     sss_packet_get_body(cctx->creq->out, &body, &blen);
-    ((uint32_t *)body)[0] = 1; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
-    ((uint32_t *)body)[2] = (uint32_t) SSS_ID_TYPE_GID;
-    memcpy(&body[3*sizeof(uint32_t)], name.str, name.len);
+    SAFEALIGN_SETMEM_UINT32(body, 1, &pctr); /* num results */
+    SAFEALIGN_SETMEM_UINT32(body + pctr, 0, &pctr); /* reserved */
+    SAFEALIGN_SETMEM_UINT32(body + pctr, SSS_ID_TYPE_GID, &pctr);
+    memcpy(&body[pctr], name.str, name.len);
 
     sss_packet_set_error(cctx->creq->out, EOK);
     sss_cmd_done(cctx, cmdctx);
diff --git a/src/responder/nss/nsssrv_mmap_cache.c b/src/responder/nss/nsssrv_mmap_cache.c
index 8655a1a..36110d6 100644
--- a/src/responder/nss/nsssrv_mmap_cache.c
+++ b/src/responder/nss/nsssrv_mmap_cache.c
@@ -539,7 +539,7 @@ static struct sss_mc_rec *sss_mc_find_record(struct sss_mc_ctx *mcc,
             return NULL;
         }
 
-        name_ptr = *((rel_ptr_t *)rec->data);
+        safealign_memcpy(&name_ptr, rec->data, sizeof(rel_ptr_t), NULL);
         if (key->len > strs_len
             || (name_ptr + key->len) > (strs_offset + strs_len)
             || (uint8_t *)rec->data + strs_offset + strs_len > max_addr) {
diff --git a/src/responder/nss/nsssrv_netgroup.c b/src/responder/nss/nsssrv_netgroup.c
index 3fc4b64..0028a88 100644
--- a/src/responder/nss/nsssrv_netgroup.c
+++ b/src/responder/nss/nsssrv_netgroup.c
@@ -680,8 +680,12 @@ static void nss_cmd_setnetgrent_done(struct tevent_req *req)
             }
 
             sss_packet_get_body(packet, &body, &blen);
-            ((uint32_t *)body)[0] = 1; /* Got some results */
-            ((uint32_t *)body)[1] = 0; /* reserved */
+
+            /* Got some results. */
+            SAFEALIGN_SETMEM_UINT32(body, 1, NULL);
+
+            /* reserved */
+            SAFEALIGN_SETMEM_UINT32(body + sizeof(uint32_t), 0, NULL);
         }
 
         sss_cmd_done(cmdctx->cctx, cmdctx);
@@ -842,7 +846,7 @@ static errno_t nss_cmd_getnetgrent_process(struct nss_cmd_ctx *cmdctx,
     if (blen != sizeof(uint32_t)) {
         return EINVAL;
     }
-    num = *((uint32_t *)body);
+    SAFEALIGN_COPY_UINT32(&num, body, NULL);
 
     /* create response packet */
     ret = sss_packet_new(client->creq, 0,
@@ -981,8 +985,12 @@ static errno_t nss_cmd_retnetgrent(struct cli_ctx *client,
     }
 
     sss_packet_get_body(packet, &body, &blen);
-    ((uint32_t *)body)[0] = num; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
+
+    /* num results */
+    SAFEALIGN_COPY_UINT32(body, &num, NULL);
+
+    /* reserved */
+    SAFEALIGN_SETMEM_UINT32(body + sizeof(uint32_t), 0, NULL);
 
     return EOK;
 }
diff --git a/src/responder/nss/nsssrv_services.c b/src/responder/nss/nsssrv_services.c
index 390e84e..80c59e2 100644
--- a/src/responder/nss/nsssrv_services.c
+++ b/src/responder/nss/nsssrv_services.c
@@ -769,8 +769,11 @@ done:
         return ENOENT;
     }
 
-    ((uint32_t *)body)[0] = num; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
+    /* num results */
+    SAFEALIGN_COPY_UINT32(body, &num, NULL);
+
+    /* reserved */
+    SAFEALIGN_SETMEM_UINT32(body + sizeof(uint32_t), 0, NULL);
 
     return ret;
 }
@@ -1734,7 +1737,7 @@ nss_cmd_getservent_immediate(struct nss_cmd_ctx *cmdctx)
     if (blen != sizeof(uint32_t)) {
         return EINVAL;
     }
-    num = *((uint32_t *)body);
+    SAFEALIGN_COPY_UINT32(&num, body, NULL);
 
     /* create response packet */
     ret = sss_packet_new(cctx->creq, 0,
-- 
1.7.11.2

