Blame SOURCES/httpd-2.4.6-CVE-2016-0736.patch

008793
diff --git a/modules/session/mod_session_crypto.c b/modules/session/mod_session_crypto.c
008793
index 4d65bb8..9231a5e 100644
008793
--- a/modules/session/mod_session_crypto.c
008793
+++ b/modules/session/mod_session_crypto.c
008793
@@ -18,6 +18,7 @@
008793
 #include "apu_version.h"
008793
 #include "apr_base64.h"                /* for apr_base64_decode et al */
008793
 #include "apr_lib.h"
008793
+#include "apr_md5.h"
008793
 #include "apr_strings.h"
008793
 #include "http_log.h"
008793
 #include "http_core.h"
008793
@@ -57,6 +58,146 @@ typedef struct {
008793
     int library_set;
008793
 } session_crypto_conf;
008793
 
008793
+/* Wrappers around apr_siphash24() and apr_crypto_equals(),
008793
+ * available in APU-1.6/APR-2.0 only.
008793
+ */
008793
+#if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 6)
008793
+
008793
+#include "apr_siphash.h"
008793
+
008793
+#define AP_SIPHASH_DSIZE    APR_SIPHASH_DSIZE
008793
+#define AP_SIPHASH_KSIZE    APR_SIPHASH_KSIZE
008793
+#define ap_siphash24_auth   apr_siphash24_auth
008793
+
008793
+#define ap_crypto_equals    apr_crypto_equals
008793
+
008793
+#else
008793
+
008793
+#define AP_SIPHASH_DSIZE    8
008793
+#define AP_SIPHASH_KSIZE    16
008793
+
008793
+#define ROTL64(x, n) (((x) << (n)) | ((x) >> (64 - (n))))
008793
+
008793
+#define U8TO64_LE(p) \
008793
+    (((apr_uint64_t)((p)[0])      ) | \
008793
+     ((apr_uint64_t)((p)[1]) <<  8) | \
008793
+     ((apr_uint64_t)((p)[2]) << 16) | \
008793
+     ((apr_uint64_t)((p)[3]) << 24) | \
008793
+     ((apr_uint64_t)((p)[4]) << 32) | \
008793
+     ((apr_uint64_t)((p)[5]) << 40) | \
008793
+     ((apr_uint64_t)((p)[6]) << 48) | \
008793
+     ((apr_uint64_t)((p)[7]) << 56))
008793
+
008793
+#define U64TO8_LE(p, v) \
008793
+do { \
008793
+    (p)[0] = (unsigned char)((v)      ); \
008793
+    (p)[1] = (unsigned char)((v) >>  8); \
008793
+    (p)[2] = (unsigned char)((v) >> 16); \
008793
+    (p)[3] = (unsigned char)((v) >> 24); \
008793
+    (p)[4] = (unsigned char)((v) >> 32); \
008793
+    (p)[5] = (unsigned char)((v) >> 40); \
008793
+    (p)[6] = (unsigned char)((v) >> 48); \
008793
+    (p)[7] = (unsigned char)((v) >> 56); \
008793
+} while (0)
008793
+
008793
+#define SIPROUND() \
008793
+do { \
008793
+    v0 += v1; v1=ROTL64(v1,13); v1 ^= v0; v0=ROTL64(v0,32); \
008793
+    v2 += v3; v3=ROTL64(v3,16); v3 ^= v2; \
008793
+    v0 += v3; v3=ROTL64(v3,21); v3 ^= v0; \
008793
+    v2 += v1; v1=ROTL64(v1,17); v1 ^= v2; v2=ROTL64(v2,32); \
008793
+} while(0)
008793
+
008793
+static apr_uint64_t ap_siphash24(const void *src, apr_size_t len,
008793
+                                 const unsigned char key[AP_SIPHASH_KSIZE])
008793
+{
008793
+    const unsigned char *ptr, *end;
008793
+    apr_uint64_t v0, v1, v2, v3, m;
008793
+    apr_uint64_t k0, k1;
008793
+    unsigned int rem;
008793
+
008793
+    k0 = U8TO64_LE(key + 0);
008793
+    k1 = U8TO64_LE(key + 8);
008793
+    v3 = k1 ^ (apr_uint64_t)0x7465646279746573ULL;
008793
+    v2 = k0 ^ (apr_uint64_t)0x6c7967656e657261ULL;
008793
+    v1 = k1 ^ (apr_uint64_t)0x646f72616e646f6dULL;
008793
+    v0 = k0 ^ (apr_uint64_t)0x736f6d6570736575ULL;
008793
+
008793
+    rem = (unsigned int)(len & 0x7);
008793
+    for (ptr = src, end = ptr + len - rem; ptr < end; ptr += 8) {
008793
+        m = U8TO64_LE(ptr);
008793
+        v3 ^= m;
008793
+        SIPROUND();
008793
+        SIPROUND();
008793
+        v0 ^= m;
008793
+    }
008793
+    m = (apr_uint64_t)(len & 0xff) << 56;
008793
+    switch (rem) {
008793
+        case 7: m |= (apr_uint64_t)ptr[6] << 48;
008793
+        case 6: m |= (apr_uint64_t)ptr[5] << 40;
008793
+        case 5: m |= (apr_uint64_t)ptr[4] << 32;
008793
+        case 4: m |= (apr_uint64_t)ptr[3] << 24;
008793
+        case 3: m |= (apr_uint64_t)ptr[2] << 16;
008793
+        case 2: m |= (apr_uint64_t)ptr[1] << 8;
008793
+        case 1: m |= (apr_uint64_t)ptr[0];
008793
+        case 0: break;
008793
+    }
008793
+    v3 ^= m;
008793
+    SIPROUND();
008793
+    SIPROUND();
008793
+    v0 ^= m;
008793
+
008793
+    v2 ^= 0xff;
008793
+    SIPROUND();
008793
+    SIPROUND();
008793
+    SIPROUND();
008793
+    SIPROUND();
008793
+
008793
+    return v0 ^ v1 ^ v2 ^ v3;
008793
+}
008793
+
008793
+static void ap_siphash24_auth(unsigned char out[AP_SIPHASH_DSIZE],
008793
+                              const void *src, apr_size_t len,
008793
+                              const unsigned char key[AP_SIPHASH_KSIZE])
008793
+{
008793
+    apr_uint64_t h;
008793
+    h = ap_siphash24(src, len, key);
008793
+    U64TO8_LE(out, h);
008793
+}
008793
+
008793
+static int ap_crypto_equals(const void *buf1, const void *buf2,
008793
+                            apr_size_t size)
008793
+{
008793
+    const unsigned char *p1 = buf1;
008793
+    const unsigned char *p2 = buf2;
008793
+    unsigned char diff = 0;
008793
+    apr_size_t i;
008793
+
008793
+    for (i = 0; i < size; ++i) {
008793
+        diff |= p1[i] ^ p2[i];
008793
+    }
008793
+
008793
+    return 1 & ((diff - 1) >> 8);
008793
+}
008793
+
008793
+#endif
008793
+
008793
+static void compute_auth(const void *src, apr_size_t len,
008793
+                         const char *passphrase, apr_size_t passlen,
008793
+                         unsigned char auth[AP_SIPHASH_DSIZE])
008793
+{
008793
+    unsigned char key[APR_MD5_DIGESTSIZE];
008793
+
008793
+    /* XXX: if we had a way to get the raw bytes from an apr_crypto_key_t
008793
+     *      we could use them directly (not available in APR-1.5.x).
008793
+     * MD5 is 128bit too, so use it to get a suitable siphash key
008793
+     * from the passphrase.
008793
+     */
008793
+    apr_md5(key, passphrase, passlen);
008793
+
008793
+    ap_siphash24_auth(auth, src, len, key);
008793
+}
008793
+
008793
 /**
008793
  * Initialise the encryption as per the current config.
008793
  *
008793
@@ -128,21 +269,14 @@ static apr_status_t encrypt_string(request_rec * r, const apr_crypto_t *f,
008793
     apr_crypto_block_t *block = NULL;
008793
     unsigned char *encrypt = NULL;
008793
     unsigned char *combined = NULL;
008793
-    apr_size_t encryptlen, tlen;
008793
+    apr_size_t encryptlen, tlen, combinedlen;
008793
     char *base64;
008793
     apr_size_t blockSize = 0;
008793
     const unsigned char *iv = NULL;
008793
     apr_uuid_t salt;
008793
     apr_crypto_block_key_type_e *cipher;
008793
     const char *passphrase;
008793
-
008793
-    /* by default, return an empty string */
008793
-    *out = "";
008793
-
008793
-    /* don't attempt to encrypt an empty string, trying to do so causes a segfault */
008793
-    if (!in || !*in) {
008793
-        return APR_SUCCESS;
008793
-    }
008793
+    apr_size_t passlen;
008793
 
008793
     /* use a uuid as a salt value, and prepend it to our result */
008793
     apr_uuid_get(&salt);
008793
@@ -152,9 +286,9 @@ static apr_status_t encrypt_string(request_rec * r, const apr_crypto_t *f,
008793
     }
008793
 
008793
     /* encrypt using the first passphrase in the list */
008793
-    passphrase = APR_ARRAY_IDX(dconf->passphrases, 0, char *);
008793
-    res = apr_crypto_passphrase(&key, &ivSize, passphrase,
008793
-            strlen(passphrase),
008793
+    passphrase = APR_ARRAY_IDX(dconf->passphrases, 0, const char *);
008793
+    passlen = strlen(passphrase);
008793
+    res = apr_crypto_passphrase(&key, &ivSize, passphrase, passlen,
008793
             (unsigned char *) (&salt), sizeof(apr_uuid_t),
008793
             *cipher, APR_MODE_CBC, 1, 4096, f, r->pool);
008793
     if (APR_STATUS_IS_ENOKEY(res)) {
008793
@@ -183,8 +317,9 @@ static apr_status_t encrypt_string(request_rec * r, const apr_crypto_t *f,
008793
     }
008793
 
008793
     /* encrypt the given string */
008793
-    res = apr_crypto_block_encrypt(&encrypt, &encryptlen, (unsigned char *)in,
008793
-            strlen(in), block);
008793
+    res = apr_crypto_block_encrypt(&encrypt, &encryptlen,
008793
+                                   (const unsigned char *)in, strlen(in),
008793
+                                   block);
008793
     if (APR_SUCCESS != res) {
008793
         ap_log_rerror(APLOG_MARK, APLOG_ERR, res, r, APLOGNO(01830)
008793
                 "apr_crypto_block_encrypt failed");
008793
@@ -198,18 +333,20 @@ static apr_status_t encrypt_string(request_rec * r, const apr_crypto_t *f,
008793
     }
008793
     encryptlen += tlen;
008793
 
008793
-    /* prepend the salt and the iv to the result */
008793
-    combined = apr_palloc(r->pool, ivSize + encryptlen + sizeof(apr_uuid_t));
008793
-    memcpy(combined, &salt, sizeof(apr_uuid_t));
008793
-    memcpy(combined + sizeof(apr_uuid_t), iv, ivSize);
008793
-    memcpy(combined + sizeof(apr_uuid_t) + ivSize, encrypt, encryptlen);
008793
-
008793
-    /* base64 encode the result */
008793
-    base64 = apr_palloc(r->pool, apr_base64_encode_len(ivSize + encryptlen +
008793
-                    sizeof(apr_uuid_t) + 1)
008793
-            * sizeof(char));
008793
-    apr_base64_encode(base64, (const char *) combined,
008793
-            ivSize + encryptlen + sizeof(apr_uuid_t));
008793
+    /* prepend the salt and the iv to the result (keep room for the MAC) */
008793
+    combinedlen = AP_SIPHASH_DSIZE + sizeof(apr_uuid_t) + ivSize + encryptlen;
008793
+    combined = apr_palloc(r->pool, combinedlen);
008793
+    memcpy(combined + AP_SIPHASH_DSIZE, &salt, sizeof(apr_uuid_t));
008793
+    memcpy(combined + AP_SIPHASH_DSIZE + sizeof(apr_uuid_t), iv, ivSize);
008793
+    memcpy(combined + AP_SIPHASH_DSIZE + sizeof(apr_uuid_t) + ivSize,
008793
+           encrypt, encryptlen);
008793
+    /* authenticate the whole salt+IV+ciphertext with a leading MAC */
008793
+    compute_auth(combined + AP_SIPHASH_DSIZE, combinedlen - AP_SIPHASH_DSIZE,
008793
+                 passphrase, passlen, combined);
008793
+
008793
+    /* base64 encode the result (APR handles the trailing '\0') */
008793
+    base64 = apr_palloc(r->pool, apr_base64_encode_len(combinedlen));
008793
+    apr_base64_encode(base64, (const char *) combined, combinedlen);
008793
     *out = base64;
008793
 
008793
     return res;
008793
@@ -234,6 +371,7 @@ static apr_status_t decrypt_string(request_rec * r, const apr_crypto_t *f,
008793
     char *decoded;
008793
     apr_size_t blockSize = 0;
008793
     apr_crypto_block_key_type_e *cipher;
008793
+    unsigned char auth[AP_SIPHASH_DSIZE];
008793
     int i = 0;
008793
 
008793
     /* strip base64 from the string */
008793
@@ -241,6 +379,13 @@ static apr_status_t decrypt_string(request_rec * r, const apr_crypto_t *f,
008793
     decodedlen = apr_base64_decode(decoded, in);
008793
     decoded[decodedlen] = '\0';
008793
 
008793
+    /* sanity check - decoded too short? */
008793
+    if (decodedlen < (AP_SIPHASH_DSIZE + sizeof(apr_uuid_t))) {
008793
+        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO()
008793
+                "too short to decrypt, aborting");
008793
+        return APR_ECRYPT;
008793
+    }
008793
+
008793
     res = crypt_init(r, f, &cipher, dconf);
008793
     if (res != APR_SUCCESS) {
008793
         return res;
008793
@@ -249,14 +394,25 @@ static apr_status_t decrypt_string(request_rec * r, const apr_crypto_t *f,
008793
     /* try each passphrase in turn */
008793
     for (; i < dconf->passphrases->nelts; i++) {
008793
         const char *passphrase = APR_ARRAY_IDX(dconf->passphrases, i, char *);
008793
-        apr_size_t len = decodedlen;
008793
-        char *slider = decoded;
008793
+        apr_size_t passlen = strlen(passphrase);
008793
+        apr_size_t len = decodedlen - AP_SIPHASH_DSIZE;
008793
+        unsigned char *slider = (unsigned char *)decoded + AP_SIPHASH_DSIZE;
008793
+
008793
+        /* Verify authentication of the whole salt+IV+ciphertext by computing
008793
+         * the MAC and comparing it (timing safe) with the one in the payload.
008793
+         */
008793
+        compute_auth(slider, len, passphrase, passlen, auth);
008793
+        if (!ap_crypto_equals(auth, decoded, AP_SIPHASH_DSIZE)) {
008793
+            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, res, r, APLOGNO()
008793
+                    "auth does not match, skipping");
008793
+            continue;
008793
+        }
008793
 
008793
         /* encrypt using the first passphrase in the list */
008793
-        res = apr_crypto_passphrase(&key, &ivSize, passphrase,
008793
-                strlen(passphrase),
008793
-                (unsigned char *)decoded, sizeof(apr_uuid_t),
008793
-                *cipher, APR_MODE_CBC, 1, 4096, f, r->pool);
008793
+        res = apr_crypto_passphrase(&key, &ivSize, passphrase, passlen,
008793
+                                    slider, sizeof(apr_uuid_t),
008793
+                                    *cipher, APR_MODE_CBC, 1, 4096,
008793
+                                    f, r->pool);
008793
         if (APR_STATUS_IS_ENOKEY(res)) {
008793
             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, res, r, APLOGNO(01832)
008793
                     "the passphrase '%s' was empty", passphrase);
008793
@@ -279,7 +435,7 @@ static apr_status_t decrypt_string(request_rec * r, const apr_crypto_t *f,
008793
         }
008793
 
008793
         /* sanity check - decoded too short? */
008793
-        if (decodedlen < (sizeof(apr_uuid_t) + ivSize)) {
008793
+        if (len < (sizeof(apr_uuid_t) + ivSize)) {
008793
             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, r, APLOGNO(01836)
008793
                     "too short to decrypt, skipping");
008793
             res = APR_ECRYPT;
008793
@@ -290,8 +446,8 @@ static apr_status_t decrypt_string(request_rec * r, const apr_crypto_t *f,
008793
         slider += sizeof(apr_uuid_t);
008793
         len -= sizeof(apr_uuid_t);
008793
 
008793
-        res = apr_crypto_block_decrypt_init(&block, &blockSize, (unsigned char *)slider, key,
008793
-                r->pool);
008793
+        res = apr_crypto_block_decrypt_init(&block, &blockSize, slider, key,
008793
+                                            r->pool);
008793
         if (APR_SUCCESS != res) {
008793
             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, res, r, APLOGNO(01837)
008793
                     "apr_crypto_block_decrypt_init failed");
008793
@@ -304,7 +460,7 @@ static apr_status_t decrypt_string(request_rec * r, const apr_crypto_t *f,
008793
 
008793
         /* decrypt the given string */
008793
         res = apr_crypto_block_decrypt(&decrypted, &decryptedlen,
008793
-                (unsigned char *)slider, len, block);
008793
+                                       slider, len, block);
008793
         if (res) {
008793
             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, res, r, APLOGNO(01838)
008793
                     "apr_crypto_block_decrypt failed");
008793