__vsprintf_chk.cpp revision 4035b7a32155eac46f3f3782774deb5967ea2b54
19b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich/* 29b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * Copyright (C) 2012 The Android Open Source Project 39b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * All rights reserved. 49b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * 59b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * Redistribution and use in source and binary forms, with or without 69b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * modification, are permitted provided that the following conditions 79b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * are met: 89b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * * Redistributions of source code must retain the above copyright 99b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * notice, this list of conditions and the following disclaimer. 109b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * * Redistributions in binary form must reproduce the above copyright 119b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * notice, this list of conditions and the following disclaimer in 129b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * the documentation and/or other materials provided with the 139b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * distribution. 149b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * 159b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 169b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 179b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 189b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 199b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 209b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 219b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 229b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 239b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 249b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 259b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 269b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * SUCH DAMAGE. 279b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich */ 289b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich 299b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich#include <stdio.h> 309b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich#include <stdlib.h> 319b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich#include <stdarg.h> 329b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich#include <private/logd.h> 339b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich 349b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich/* 359b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * Runtime implementation of __builtin____vsprintf_chk. 369b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * 379b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * See 389b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html 399b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html 409b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * for details. 419b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * 429b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * This vsprintf check is called if _FORTIFY_SOURCE is defined and 439b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * greater than 0. 449b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich */ 454035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughesextern "C" int __vsprintf_chk( 469b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich char *dest, 474035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes int /*flags*/, 489b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich size_t dest_len_from_compiler, 499b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich const char *format, 509b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich va_list va) 519b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich{ 529b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich int ret = vsnprintf(dest, dest_len_from_compiler, format, va); 539b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich 549b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich if ((size_t) ret >= dest_len_from_compiler) { 559b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich __libc_android_log_print(ANDROID_LOG_FATAL, "libc", 569b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich "*** vsprintf buffer overflow detected ***\n"); 579b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich abort(); 589b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich } 599b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich 609b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich return ret; 619b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich} 624035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes 634035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes/* 644035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * Runtime implementation of __builtin____sprintf_chk. 654035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * 664035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * See 674035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html 684035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html 694035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * for details. 704035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * 714035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * This sprintf check is called if _FORTIFY_SOURCE is defined and 724035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * greater than 0. 734035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes */ 744035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughesextern "C" int __sprintf_chk( 754035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes char *dest, 764035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes int flags, 774035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes size_t dest_len_from_compiler, 784035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes const char *format, ...) 794035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes{ 804035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes va_list va; 814035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes int retval; 824035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes 834035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes va_start(va, format); 844035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes retval = __vsprintf_chk(dest, flags, 854035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes dest_len_from_compiler, format, va); 864035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes va_end(va); 874035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes 884035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes return retval; 894035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes} 90