Fix segfault while authorizing on protected buckets
Fix segfault while authorizing on protected buckets

Minimal working example
=======================

test.c
------

  #define _FORTIFY_SOURCE 2
  #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>

  struct sasl {
      const char *name;
      union {
          struct {
              unsigned long len;
              unsigned char data[1];
          } secret;
          char buffer[256];
      } password;
      void *callbacks[4];
  };

  #define STACK_MKSASL struct sasl sasl_stack;
  #define STACK_SASL (&sasl_stack)

  #define HEAP_MKSASL  struct sasl *sasl_heap = malloc(sizeof(*sasl_heap))
  #define HEAP_SASL sasl_heap

  int main(void)
  {
      STACK_MKSASL;
      HEAP_MKSASL;
      const char *str = "secret";
      printf("GCCs __bos (For stack): 0x%lx\n", __bos(STACK_SASL->password.secret.data));
      printf("GCCs __bos (For heap): 0x%lx\n", __bos(HEAP_SASL->password.secret.data));
      strcpy((char*)STACK_SASL->password.secret.data, str);
      strcpy((char*)HEAP_SASL->password.secret.data, str);
      return 0;
  }

$ gcc -O2 -o test test.c
In file included from /usr/include/string.h:642:0,
                 from test.c:3:
In function ‘strcpy’,
    inlined from ‘main’ at test.c:31:11:
/usr/include/x86_64-linux-gnu/bits/string3.h:105:3: warning: call to __builtin___strcpy_chk will always overflow destination buffer [enabled by default]
In function ‘strcpy’,
    inlined from ‘main’ at test.c:32:11:
/usr/include/x86_64-linux-gnu/bits/string3.h:105:3: warning: call to __builtin___strcpy_chk will always overflow destination buffer [enabled by default]

$ ./test
GCCs __bos (For stack): 0x1
GCCs __bos (For heap): 0x1
*** buffer overflow detected ***: ./test terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x37)[0x7f1885534077]
/lib/x86_64-linux-gnu/libc.so.6(+0xebf30)[0x7f1885532f30]
./test[0x400525]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xfd)[0x7f1885465ead]
./test[0x40056d]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:03 13114118                           /home/avsej/tmp/test
00600000-00601000 rw-p 00000000 08:03 13114118                           /home/avsej/tmp/test
014e9000-0150a000 rw-p 00000000 00:00 0                                  [heap]
7f1885231000-7f1885246000 r-xp 00000000 08:01 2360249                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f1885246000-7f1885446000 ---p 00015000 08:01 2360249                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f1885446000-7f1885447000 rw-p 00015000 08:01 2360249                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f1885447000-7f18855c4000 r-xp 00000000 08:01 2359335                    /lib/x86_64-linux-gnu/libc-2.13.so
7f18855c4000-7f18857c4000 ---p 0017d000 08:01 2359335                    /lib/x86_64-linux-gnu/libc-2.13.so
7f18857c4000-7f18857c8000 r--p 0017d000 08:01 2359335                    /lib/x86_64-linux-gnu/libc-2.13.so
7f18857c8000-7f18857c9000 rw-p 00181000 08:01 2359335                    /lib/x86_64-linux-gnu/libc-2.13.so
7f18857c9000-7f18857ce000 rw-p 00000000 00:00 0
7f18857ce000-7f18857ed000 r-xp 00000000 08:01 2363016                    /lib/x86_64-linux-gnu/ld-2.13.so
7f18859cc000-7f18859cf000 rw-p 00000000 00:00 0
7f18859ea000-7f18859ed000 rw-p 00000000 00:00 0
7f18859ed000-7f18859ee000 r--p 0001f000 08:01 2363016                    /lib/x86_64-linux-gnu/ld-2.13.so
7f18859ee000-7f18859ef000 rw-p 00020000 08:01 2363016                    /lib/x86_64-linux-gnu/ld-2.13.so
7f18859ef000-7f18859f0000 rw-p 00000000 00:00 0
7fff268c0000-7fff268e1000 rw-p 00000000 00:00 0                          [stack]
7fff26900000-7fff26901000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

Change-Id: I4dc88636a15bcb380ec28579760d70a588fd7947
Reviewed-on: http://review.couchbase.org/14641
Reviewed-by: Matt Ingenthron <1fa2ef4755a9226cb9a0a4840bd89b158ac71391@couchbase.com>
Reviewed-by: Trond Norbye <60edd2ef23891a753f231b0c6f161dc634079a93@gmail.com>
Tested-by: Sergey Avseyev <87f6d5e4fd3644c3c20800cde7fd3ad1569370b3@gmail.com>

kojiromike/libcouchbase,PureSwift/libcouchbase,couchbase/libcouchbase,PureSwift/libcouchbase,maxim-ky/libcouchbase,senthilkumaranb/libcouchbase,maxim-ky/libcouchbase,signmotion/libcouchbase,avsej/libcouchbase,couchbase/libcouchbase,uvenum/libcouchbase,senthilkumaranb/libcouchbase,uvenum/libcouchbase,couchbase/libcouchbase,senthilkumaranb/libcouchbase,kojiromike/libcouchbase,mnunberg/libcouchbase,maxim-ky/libcouchbase,trondn/libcouchbase,uvenum/libcouchbase,avsej/libcouchbase,maxim-ky/libcouchbase,avsej/libcouchbase,avsej/libcouchbase,trondn/libcouchbase,PureSwift/libcouchbase,kojiromike/libcouchbase,avsej/libcouchbase,couchbase/libcouchbase,trondn/libcouchbase,senthilkumaranb/libcouchbase,signmotion/libcouchbase,uvenum/libcouchbase,couchbase/libcouchbase,mnunberg/libcouchbase,signmotion/libcouchbase,avsej/libcouchbase,mody/libcouchbase,trondn/libcouchbase,signmotion/libcouchbase,kojiromike/libcouchbase,couchbase/libcouchbase,mnunberg/libcouchbase,mody/libcouchbase,couchbase/libcouchbase,PureSwift/libcouchbase,mnunberg/libcouchbase,trondn/libcouchbase,avsej/libcouchbase,mnunberg/libcouchbase,uvenum/libcouchbase,mody/libcouchbase,mody/libcouchbase