>From d785a0ca0175c4d53da6163cce6f9467ec0bde8c Mon Sep 17 00:00:00 2001
From: Michal Zidek <mzidek@redhat.com>
Date: Wed, 28 Aug 2013 12:46:58 +0200
Subject: [PATCH 4/7] 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        | 52 +++++++++++++++++++----------------
 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, 70 insertions(+), 39 deletions(-)

diff --git a/src/responder/autofs/autofssrv_cmd.c b/src/responder/autofs/autofssrv_cmd.c
index e9168ea..bce8cdf 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 f2f6e90..fc0f797 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;
 }
@@ -1389,7 +1389,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));
@@ -1970,7 +1970,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,
@@ -2545,8 +2545,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;
 }
@@ -3269,7 +3269,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,
@@ -3541,7 +3541,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
@@ -3552,13 +3553,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 */
 
     return EOK;
 }
@@ -4107,6 +4109,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) {
@@ -4123,10 +4126,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;
 }
@@ -4145,6 +4148,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) {
@@ -4184,10 +4188,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;
 
@@ -4204,6 +4209,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) {
@@ -4224,10 +4230,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;
 }
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 a1c4196..9d03eec 100644
--- a/src/responder/nss/nsssrv_netgroup.c
+++ b/src/responder/nss/nsssrv_netgroup.c
@@ -683,8 +683,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);
@@ -845,7 +849,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,
@@ -984,8 +988,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 52a2f44..f3c411c 100644
--- a/src/responder/nss/nsssrv_services.c
+++ b/src/responder/nss/nsssrv_services.c
@@ -773,8 +773,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;
 }
@@ -1738,7 +1741,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

