>From 3cdb7e7640630f28ac7d766459d57bb509e9ee9e 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  | 20 +++++++---
 src/responder/nss/nsssrv_cmd.c        | 70 +++++++++++++++++++----------------
 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, 80 insertions(+), 47 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..111a19c 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,14 @@ 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]));
+
+    if (cctx->cli_protocol_version != NULL) {
+        protocol_version = cctx->cli_protocol_version->version;
+    } else {
+        protocol_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 64bb9bc..513f46a 100644
--- a/src/responder/nss/nsssrv_cmd.c
+++ b/src/responder/nss/nsssrv_cmd.c
@@ -456,8 +456,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;
 }
@@ -985,6 +985,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,
@@ -1014,10 +1015,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);
@@ -1450,7 +1451,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));
@@ -2029,7 +2030,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,
@@ -2604,8 +2605,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;
 }
@@ -3322,7 +3323,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,
@@ -3594,7 +3595,8 @@ static int fill_initgr(struct sss_packet *packet, struct ldb_result *res)
                 return EFAULT;
             }
         }
-        ((uint32_t *)body)[2 + bindex] = gid;
+        SAFEALIGN_COPY_UINT32(body + sizeof(uint32_t) * (2 + bindex),
+                              &gid, NULL);
         bindex++;
 
         /* do not add the GID of the original primary group is the user is
@@ -3605,13 +3607,14 @@ 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;
+        SAFEALIGN_COPY_UINT32(body + sizeof(uint32_t) * (2 + bindex),
+                              &orig_primary_gid, NULL);
         bindex++;
         num++;
     }
 
-    ((uint32_t *)body)[0] = num-skipped; /* num results */
-    ((uint32_t *)body)[1] = 0; /* reserved */
+    SAFEALIGN_SETMEM_UINT32(body, num - skipped, NULL); /* num results */
+    SAFEALIGN_SETMEM_UINT32(body + sizeof(uint32_t), 0, NULL); /* reserved */
     blen = (2 + bindex) * sizeof(uint32_t);
     ret = sss_packet_set_size(packet, blen);
     if (ret != EOK) {
@@ -4165,6 +4168,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) {
@@ -4181,10 +4185,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;
 }
@@ -4203,6 +4207,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) {
@@ -4242,10 +4247,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;
 
@@ -4262,6 +4268,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) {
@@ -4282,10 +4289,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;
 }
@@ -4354,6 +4361,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) {
@@ -4391,10 +4399,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 b21d217..4d55105 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, NULL);
@@ -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_COPY_UINT32(body + sizeof(uint32_t), &num, 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

