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 29e4c6b08c4e81f640afc502804d1226a3e79dc26dNick Kralevich#undef _FORTIFY_SOURCE 30e4c6b08c4e81f640afc502804d1226a3e79dc26dNick Kralevich 319b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich#include <stdio.h> 329b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich#include <stdlib.h> 339b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich#include <stdarg.h> 34eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/libc_logging.h" 359b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich 369b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich/* 379b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * Runtime implementation of __builtin____vsprintf_chk. 389b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * 399b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * See 409b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html 419b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html 429b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * for details. 439b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * 449b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * This vsprintf check is called if _FORTIFY_SOURCE is defined and 459b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich * greater than 0. 469b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich */ 47d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughesextern "C" int __vsprintf_chk(char* dest, int /*flags*/, 4868b67113a44311b3568027af5893e316f63ec556Elliott Hughes size_t dest_len_from_compiler, const char* format, va_list va) { 49d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes int result = vsnprintf(dest, dest_len_from_compiler, format, va); 50d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes if ((size_t) result >= dest_len_from_compiler) { 51d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes __fortify_chk_fail("vsprintf: prevented write past end of buffer", 0); 52d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes } 53d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes return result; 549b549c39c938f54680f282c21e6885f53254bfb0Nick Kralevich} 554035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes 564035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes/* 574035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * Runtime implementation of __builtin____sprintf_chk. 584035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * 594035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * See 604035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html 614035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html 624035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * for details. 634035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * 644035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * This sprintf check is called if _FORTIFY_SOURCE is defined and 654035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes * greater than 0. 664035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes */ 67d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughesextern "C" int __sprintf_chk(char* dest, int flags, 68d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes size_t dest_len_from_compiler, const char* format, ...) { 69d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes va_list va; 70d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes va_start(va, format); 71d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes int result = __vsprintf_chk(dest, flags, dest_len_from_compiler, format, va); 72d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes va_end(va); 73d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes return result; 744035b7a32155eac46f3f3782774deb5967ea2b54Elliott Hughes} 75