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 */ 459b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevichint __vsprintf_chk( 469b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich char *dest, 479b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich 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} 62