1965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich/* 2965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * Copyright (C) 2012 The Android Open Source Project 3965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * All rights reserved. 4965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * 5965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * Redistribution and use in source and binary forms, with or without 6965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * modification, are permitted provided that the following conditions 7965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * are met: 8965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * * Redistributions of source code must retain the above copyright 9965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * notice, this list of conditions and the following disclaimer. 10965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * * Redistributions in binary form must reproduce the above copyright 11965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * notice, this list of conditions and the following disclaimer in 12965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * the documentation and/or other materials provided with the 13965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * distribution. 14965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * 15965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * SUCH DAMAGE. 27965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich */ 28965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich 29e4c6b08c4e81f640afc502804d1226a3e79dc26dNick Kralevich#undef _FORTIFY_SOURCE 30e4c6b08c4e81f640afc502804d1226a3e79dc26dNick Kralevich 31965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich#include <stdio.h> 32965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich#include <stdlib.h> 33eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/libc_logging.h" 34965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich 35965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich/* 36965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * __fgets_chk. Called in place of fgets() when we know the 37965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * size of the buffer we're writing into. 38965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * 39965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * See 40965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html 41965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * for details. 42965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * 43965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * This fgets check is called if _FORTIFY_SOURCE is defined and 44965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich * greater than 0. 45965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich */ 46d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughesextern "C" char* __fgets_chk(char* dest, int supplied_size, 47d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes FILE* stream, size_t dest_len_from_compiler) { 48d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes if (supplied_size < 0) { 49d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes __fortify_chk_fail("fgets: buffer size < 0", 0); 50d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes } 51965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich 52d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes if (((size_t) supplied_size) > dest_len_from_compiler) { 53d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes __fortify_chk_fail("fgets: prevented write past end of buffer", 0); 54d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes } 55965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich 56d1eda33f012e46083b91e087fb79d14a5ce70f0eElliott Hughes return fgets(dest, supplied_size, stream); 57965dbc6405aa2c3170270cfc53a8d4416444fddbNick Kralevich} 58