__readlink_chk.cpp revision 42281880a8ac8614832ff918a14e4b950f35d05d
142281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay/*
242281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * Copyright (C) 2015 The Android Open Source Project
342281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * All rights reserved.
442281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay *
542281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * Redistribution and use in source and binary forms, with or without
642281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * modification, are permitted provided that the following conditions
742281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * are met:
842281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay *  * Redistributions of source code must retain the above copyright
942281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay *    notice, this list of conditions and the following disclaimer.
1042281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay *  * Redistributions in binary form must reproduce the above copyright
1142281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay *    notice, this list of conditions and the following disclaimer in
1242281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay *    the documentation and/or other materials provided with the
1342281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay *    distribution.
1442281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay *
1542281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1642281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1742281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
1842281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
1942281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2042281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2142281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2242281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2342281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2442281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2542281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2642281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay * SUCH DAMAGE.
2742281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay */
2842281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay
2942281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay#undef _FORTIFY_SOURCE
3042281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay#include <unistd.h>
3142281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay#include "private/libc_logging.h"
3242281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay
3342281880a8ac8614832ff918a14e4b950f35d05dDaniel Micayextern "C" ssize_t __readlink_chk(const char* path, char* buf, size_t size, size_t buf_size) {
3442281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay  if (__predict_false(size > buf_size)) {
3542281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay    __fortify_chk_fail("readlink: prevented write past end of buffer", 0);
3642281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay  }
3742281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay
3842281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay  if (__predict_false(size > SSIZE_MAX)) {
3942281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay    __fortify_chk_fail("readlink: size > SSIZE_MAX", 0);
4042281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay  }
4142281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay
4242281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay  return readlink(path, buf, size);
4342281880a8ac8614832ff918a14e4b950f35d05dDaniel Micay}
44