18df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich/* 28df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * Copyright (C) 2012 The Android Open Source Project 38df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * All rights reserved. 48df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * 58df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * Redistribution and use in source and binary forms, with or without 68df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * modification, are permitted provided that the following conditions 78df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * are met: 88df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * * Redistributions of source code must retain the above copyright 98df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * notice, this list of conditions and the following disclaimer. 108df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * * Redistributions in binary form must reproduce the above copyright 118df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * notice, this list of conditions and the following disclaimer in 128df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * the documentation and/or other materials provided with the 138df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * distribution. 148df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * 158df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 168df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 178df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 188df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 198df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 208df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 218df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 228df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 238df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 248df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 258df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 268df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * SUCH DAMAGE. 278df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich */ 288df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich 29e4c6b08c4e81f640afc502804d1226a3e79dc26dNick Kralevich#undef _FORTIFY_SOURCE 30e4c6b08c4e81f640afc502804d1226a3e79dc26dNick Kralevich 318df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich#include <string.h> 328df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich#include <stdlib.h> 33eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/libc_logging.h" 348df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich 358df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich/* 368df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * __strlcpy_chk. Called in place of strlcpy() when we know the 378df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * size of the buffer we're writing into. 388df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * 398df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * See 408df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html 418df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html 428df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * for details. 438df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * 448df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * This strlcpy check is called if _FORTIFY_SOURCE is defined and 458df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich * greater than 0. 468df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich */ 47d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughesextern "C" size_t __strlcpy_chk(char* dest, const char* src, 48d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes size_t supplied_size, size_t dest_len_from_compiler) { 49d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes if (__predict_false(supplied_size > dest_len_from_compiler)) { 50d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes __fortify_chk_fail("strlcpy: prevented write past end of buffer", 0); 51d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes } 528df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich 53d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes return strlcpy(dest, src, supplied_size); 548df49ad2467ec2d48f94a925162185c34bf6e68bNick Kralevich} 55