10a2301598c207fd1b50015984942fee5e8511593Nick Kralevich/*
20a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * Copyright (C) 2012 The Android Open Source Project
30a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * All rights reserved.
40a2301598c207fd1b50015984942fee5e8511593Nick Kralevich *
50a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * Redistribution and use in source and binary forms, with or without
60a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * modification, are permitted provided that the following conditions
70a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * are met:
80a2301598c207fd1b50015984942fee5e8511593Nick Kralevich *  * Redistributions of source code must retain the above copyright
90a2301598c207fd1b50015984942fee5e8511593Nick Kralevich *    notice, this list of conditions and the following disclaimer.
100a2301598c207fd1b50015984942fee5e8511593Nick Kralevich *  * Redistributions in binary form must reproduce the above copyright
110a2301598c207fd1b50015984942fee5e8511593Nick Kralevich *    notice, this list of conditions and the following disclaimer in
120a2301598c207fd1b50015984942fee5e8511593Nick Kralevich *    the documentation and/or other materials provided with the
130a2301598c207fd1b50015984942fee5e8511593Nick Kralevich *    distribution.
140a2301598c207fd1b50015984942fee5e8511593Nick Kralevich *
150a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
160a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
170a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
180a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
190a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
200a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
210a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
220a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
230a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
240a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
250a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * SUCH DAMAGE.
270a2301598c207fd1b50015984942fee5e8511593Nick Kralevich */
280a2301598c207fd1b50015984942fee5e8511593Nick Kralevich
290a2301598c207fd1b50015984942fee5e8511593Nick Kralevich#include <string.h>
300a2301598c207fd1b50015984942fee5e8511593Nick Kralevich#include <stdlib.h>
31eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/libc_logging.h"
320a2301598c207fd1b50015984942fee5e8511593Nick Kralevich
330a2301598c207fd1b50015984942fee5e8511593Nick Kralevich/*
340a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * Runtime implementation of __builtin____strncat_chk.
350a2301598c207fd1b50015984942fee5e8511593Nick Kralevich *
360a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * See
370a2301598c207fd1b50015984942fee5e8511593Nick Kralevich *   http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html
380a2301598c207fd1b50015984942fee5e8511593Nick Kralevich *   http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
390a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * for details.
400a2301598c207fd1b50015984942fee5e8511593Nick Kralevich *
410a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * This strncat check is called if _FORTIFY_SOURCE is defined and
420a2301598c207fd1b50015984942fee5e8511593Nick Kralevich * greater than 0.
430a2301598c207fd1b50015984942fee5e8511593Nick Kralevich */
44d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughesextern "C" char* __strncat_chk(char* __restrict dest, const char* __restrict src,
45d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes                               size_t len, size_t dest_buf_size) {
46d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes  if (len == 0) {
47d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes    return dest;
48d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes  }
490a2301598c207fd1b50015984942fee5e8511593Nick Kralevich
50d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes  size_t dest_len = __strlen_chk(dest, dest_buf_size);
51d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes  char *d = dest + dest_len;
52d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes  dest_buf_size -= dest_len;
53cf870199d576bdfc339b7fb016c9f6fe7f2c87edNick Kralevich
54d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes  while (*src != '\0') {
55d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes    *d++ = *src++;
56d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes    len--; dest_buf_size--;
57cf870199d576bdfc339b7fb016c9f6fe7f2c87edNick Kralevich
58d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes    if (__predict_false(dest_buf_size == 0)) {
59d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes      __fortify_chk_fail("strncat: prevented write past end of buffer",
60d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes                         BIONIC_EVENT_STRNCAT_BUFFER_OVERFLOW);
61d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes    }
6276656afc6dd069fcfda5768e6e54bb85e4e99942Nick Kralevich
63d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes    if (len == 0) {
64d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes      break;
650a2301598c207fd1b50015984942fee5e8511593Nick Kralevich    }
66d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes  }
670a2301598c207fd1b50015984942fee5e8511593Nick Kralevich
68d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes  *d = '\0';
69d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes  return dest;
700a2301598c207fd1b50015984942fee5e8511593Nick Kralevich}
71