|
Packit |
534379 |
// Copyright(c) 2019-2020, Intel Corporation
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
534379 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
534379 |
// this list of conditions and the following disclaimer.
|
|
Packit |
534379 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
534379 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
534379 |
// and/or other materials provided with the distribution.
|
|
Packit |
534379 |
// * Neither the name of Intel Corporation nor the names of its contributors
|
|
Packit |
534379 |
// may be used to endorse or promote products derived from this software
|
|
Packit |
534379 |
// without specific prior written permission.
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
534379 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
534379 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
534379 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
534379 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
534379 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
534379 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
534379 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
534379 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
534379 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
534379 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#define _GNU_SOURCE
|
|
Packit |
534379 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
534379 |
#include <config.h>
|
|
Packit |
534379 |
#endif // HAVE_CONFIG_H
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#include <string.h>
|
|
Packit |
534379 |
#include <ctype.h>
|
|
Packit |
534379 |
#include <sys/types.h>
|
|
Packit |
534379 |
#include <sys/stat.h>
|
|
Packit |
534379 |
#include <unistd.h>
|
|
Packit |
534379 |
#include <limits.h>
|
|
Packit |
534379 |
#include <fcntl.h>
|
|
Packit |
534379 |
#include <opae/log.h>
|
|
Packit |
534379 |
|
|
Packit |
534379 |
#include "bits_utils.h"
|
|
Packit |
534379 |
|
|
Packit |
534379 |
fpga_result opae_bitstream_get_json_string(json_object *parent,
|
|
Packit |
534379 |
const char *name,
|
|
Packit |
534379 |
char **value)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
json_object *obj = NULL;
|
|
Packit |
534379 |
const char *s;
|
|
Packit |
534379 |
size_t len;
|
|
Packit |
534379 |
char *p;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (!json_object_object_get_ex(parent,
|
|
Packit |
534379 |
name,
|
|
Packit |
534379 |
&obj)) {
|
|
Packit |
534379 |
return FPGA_EXCEPTION;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (!json_object_is_type(obj, json_type_string)) {
|
|
Packit |
534379 |
OPAE_ERR("metadata: \"%s\" key not string", name);
|
|
Packit |
534379 |
return FPGA_EXCEPTION;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
s = json_object_get_string(obj);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
len = strlen(s);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
*value = malloc(len + 1);
|
|
Packit |
534379 |
if (!*value) {
|
|
Packit |
534379 |
OPAE_ERR("malloc failed");
|
|
Packit |
534379 |
return FPGA_NO_MEMORY;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
memcpy(*value, s, len);
|
|
Packit |
534379 |
p = *value;
|
|
Packit |
534379 |
p[len] = '\0';
|
|
Packit |
534379 |
|
|
Packit |
534379 |
return FPGA_OK;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
fpga_result opae_bitstream_get_json_int(json_object *parent,
|
|
Packit |
534379 |
const char *name,
|
|
Packit |
534379 |
int *value)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
json_object *obj = NULL;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (!json_object_object_get_ex(parent,
|
|
Packit |
534379 |
name,
|
|
Packit |
534379 |
&obj)) {
|
|
Packit |
534379 |
return FPGA_EXCEPTION;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (!json_object_is_type(obj, json_type_int)) {
|
|
Packit |
534379 |
return FPGA_EXCEPTION;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
*value = json_object_get_int(obj);
|
|
Packit |
534379 |
return FPGA_OK;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
fpga_result opae_bitstream_get_json_double(json_object *parent,
|
|
Packit |
534379 |
const char *name,
|
|
Packit |
534379 |
double *value)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
json_object *obj = NULL;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (!json_object_object_get_ex(parent,
|
|
Packit |
534379 |
name,
|
|
Packit |
534379 |
&obj)) {
|
|
Packit |
534379 |
return FPGA_EXCEPTION;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (!json_object_is_type(obj, json_type_double)) {
|
|
Packit |
534379 |
return FPGA_EXCEPTION;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
*value = json_object_get_double(obj);
|
|
Packit |
534379 |
return FPGA_OK;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
STATIC bool opae_bitstream_path_invalid_chars(const char *path,
|
|
Packit |
534379 |
size_t len)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
while (*path) {
|
|
Packit |
534379 |
int ch = *path;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// check for non-printable chars
|
|
Packit |
534379 |
if (!isprint(ch))
|
|
Packit |
534379 |
return true;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// check for URL encoding
|
|
Packit |
534379 |
if ((ch == '%') &&
|
|
Packit |
534379 |
(len >= 3) &&
|
|
Packit |
534379 |
(isxdigit(*(path+1)) && isxdigit(*(path+2))))
|
|
Packit |
534379 |
return true;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
++path;
|
|
Packit |
534379 |
--len;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
return false;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
STATIC bool opae_bitstream_path_not_file(const char *path)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
struct stat sb;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (stat(path, &sb) < 0)
|
|
Packit |
534379 |
return true; // can't determine
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (!S_ISREG(sb.st_mode))
|
|
Packit |
534379 |
return true;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
return false;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
STATIC bool opae_bitstream_path_contains_dotdot(const char *path,
|
|
Packit |
534379 |
size_t len)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
if (len >= 3) {
|
|
Packit |
534379 |
// check for ../ at the start of the path.
|
|
Packit |
534379 |
if ((*path == '.') &&
|
|
Packit |
534379 |
(*(path + 1) == '.') &&
|
|
Packit |
534379 |
(*(path + 2) == '/'))
|
|
Packit |
534379 |
return true;
|
|
Packit |
534379 |
} else if (len == 2) {
|
|
Packit |
534379 |
// check for ".."
|
|
Packit |
534379 |
if ((*path == '.') &&
|
|
Packit |
534379 |
(*(path + 1) == '.'))
|
|
Packit |
534379 |
return true;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
while (*path) {
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (len >= 4) {
|
|
Packit |
534379 |
// check for /../
|
|
Packit |
534379 |
if ((*path == '/') &&
|
|
Packit |
534379 |
(*(path + 1) == '.') &&
|
|
Packit |
534379 |
(*(path + 2) == '.') &&
|
|
Packit |
534379 |
(*(path + 3) == '/'))
|
|
Packit |
534379 |
return true;
|
|
Packit |
534379 |
} else if (len == 3) {
|
|
Packit |
534379 |
// check for /.. at the end
|
|
Packit |
534379 |
if ((*path == '/') &&
|
|
Packit |
534379 |
(*(path + 1) == '.') &&
|
|
Packit |
534379 |
(*(path + 2) == '.'))
|
|
Packit |
534379 |
return true;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
++path;
|
|
Packit |
534379 |
--len;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
return false;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
STATIC bool opae_bitstream_path_contains_symlink(const char *path,
|
|
Packit |
534379 |
size_t len)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
char component[PATH_MAX] = { 0, };
|
|
Packit |
534379 |
struct stat stat_buf;
|
|
Packit |
534379 |
char *pslash;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
memcpy(component, path, len);
|
|
Packit |
534379 |
component[len] = '\0';
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (component[0] == '/') {
|
|
Packit |
534379 |
// absolute path
|
|
Packit |
534379 |
|
|
Packit |
534379 |
pslash = realpath(path, component);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// If the result of conversion through realpath() is different
|
|
Packit |
534379 |
// than the original path, then the original must have
|
|
Packit |
534379 |
// contained a symlink.
|
|
Packit |
534379 |
if (strcmp(component, path)) {
|
|
Packit |
534379 |
return true;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
} else {
|
|
Packit |
534379 |
// relative path
|
|
Packit |
534379 |
|
|
Packit |
534379 |
pslash = strrchr(component, '/');
|
|
Packit |
534379 |
|
|
Packit |
534379 |
while (pslash) {
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (fstatat(AT_FDCWD, component,
|
|
Packit |
534379 |
&stat_buf, AT_SYMLINK_NOFOLLOW)) {
|
|
Packit |
534379 |
OPAE_ERR("fstatat failed.");
|
|
Packit |
534379 |
return true;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (S_ISLNK(stat_buf.st_mode))
|
|
Packit |
534379 |
return true;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
*pslash = '\0';
|
|
Packit |
534379 |
pslash = strrchr(component, '/');
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (fstatat(AT_FDCWD, component,
|
|
Packit |
534379 |
&stat_buf, AT_SYMLINK_NOFOLLOW)) {
|
|
Packit |
534379 |
OPAE_ERR("fstatat failed.");
|
|
Packit |
534379 |
return true;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (S_ISLNK(stat_buf.st_mode))
|
|
Packit |
534379 |
return true;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
return false;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
bool opae_bitstream_path_is_valid(const char *path,
|
|
Packit |
534379 |
uint32_t flags)
|
|
Packit |
534379 |
{
|
|
Packit |
534379 |
size_t len;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
// check for NULL / empty string
|
|
Packit |
534379 |
if (!path || (*path == '\0'))
|
|
Packit |
534379 |
return false;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
len = strlen(path);
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (opae_bitstream_path_invalid_chars(path, len))
|
|
Packit |
534379 |
return false;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if (opae_bitstream_path_not_file(path))
|
|
Packit |
534379 |
return false;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if ((flags & OPAE_BITSTREAM_PATH_NO_PARENT) &&
|
|
Packit |
534379 |
opae_bitstream_path_contains_dotdot(path, len))
|
|
Packit |
534379 |
return false;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
if ((flags & OPAE_BITSTREAM_PATH_NO_SYMLINK) &&
|
|
Packit |
534379 |
opae_bitstream_path_contains_symlink(path, len))
|
|
Packit |
534379 |
return false;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
return true;
|
|
Packit |
534379 |
}
|