// SPDX-License-Identifier: GPL-2.0-only /* * Copyright 2012-2016 by the PaX Team <pageexec@freemail.hu> * Copyright 2016 by Emese Revfy <re.emese@gmail.com> * * Note: the choice of the license means that the compilation process is * NOT 'eligible' as defined by gcc's library exception to the GPL v3, * but for the kernel it doesn't matter since it doesn't link against * any of the gcc libraries * * This gcc plugin helps generate a little bit of entropy from program state, * used throughout the uptime of the kernel. Here is an instrumentation example: * * before: * void __latent_entropy test(int argc, char *argv[]) * { * if (argc <= 1) * printf("%s: no command arguments :(\n", *argv); * else * printf("%s: %d command arguments!\n", *argv, argc - 1); * } * * after: * void __latent_entropy test(int argc, char *argv[]) * { * // latent_entropy_execute() 1. * unsigned long local_entropy; * // init_local_entropy() 1. * void *local_entropy_frameaddr; * // init_local_entropy() 3. * unsigned long tmp_latent_entropy; * * // init_local_entropy() 2. * local_entropy_frameaddr = __builtin_frame_address(0); * local_entropy = (unsigned long) local_entropy_frameaddr; * * // init_local_entropy() 4. * tmp_latent_entropy = latent_entropy; * // init_local_entropy() 5. * local_entropy ^= tmp_latent_entropy; * * // latent_entropy_execute() 3. * if (argc <= 1) { * // perturb_local_entropy() * local_entropy += 4623067384293424948; * printf("%s: no command arguments :(\n", *argv); * // perturb_local_entropy() * } else { * local_entropy ^= 3896280633962944730; * printf("%s: %d command arguments!\n", *argv, argc - 1); * } * * // latent_entropy_execute() 4. * tmp_latent_entropy = rol(tmp_latent_entropy, local_entropy); * latent_entropy = tmp_latent_entropy; * } * * TODO: * - add ipa pass to identify not explicitly marked candidate functions * - mix in more program state (function arguments/return values, * loop variables, etc) * - more instrumentation control via attribute parameters * * BUGS: * - none known * * Options: * -fplugin-arg-latent_entropy_plugin-disable * * Attribute: __attribute__((latent_entropy)) * The latent_entropy gcc attribute can be only on functions and variables. * If it is on a function then the plugin will instrument it. If the attribute * is on a variable then the plugin will initialize it with a random value. * The variable must be an integer, an integer array type or a structure * with integer fields.
*/
staticunsigned HOST_WIDE_INT get_random_const(void)
{ if (deterministic_seed) { unsigned HOST_WIDE_INT w = deterministic_seed;
w ^= w << 13;
w ^= w >> 7;
w ^= w << 17;
deterministic_seed = w; return deterministic_seed;
}
static tree handle_latent_entropy_attribute(tree *node, tree name,
tree args __unused, int flags __unused, bool *no_add_attrs)
{
tree type;
vec<constructor_elt, va_gc> *vals;
switch (TREE_CODE(*node)) { default:
*no_add_attrs = true;
error("%qE attribute only applies to functions and variables",
name); break;
case VAR_DECL: if (DECL_INITIAL(*node)) {
*no_add_attrs = true;
error("variable %qD with %qE attribute must not be initialized",
*node, name); break;
}
if (!TREE_STATIC(*node)) {
*no_add_attrs = true;
error("variable %qD with %qE attribute must not be local",
*node, name); break;
}
type = TREE_TYPE(*node); switch (TREE_CODE(type)) { default:
*no_add_attrs = true;
error("variable %qD with %qE attribute must be an integer or a fixed length integer array type or a fixed sized structure with integer fields",
*node, name); break;
case RECORD_TYPE: {
tree fld, lst = TYPE_FIELDS(type); unsignedint nelt = 0;
for (fld = lst; fld; nelt++, fld = TREE_CHAIN(fld)) {
tree fieldtype;
fieldtype = TREE_TYPE(fld); if (TREE_CODE(fieldtype) == INTEGER_TYPE) continue;
*no_add_attrs = true;
error("structure variable %qD with %qE attribute has a non-integer field %qE",
*node, name, fld); break;
}
if (fld) break;
vec_alloc(vals, nelt);
for (fld = lst; fld; fld = TREE_CHAIN(fld)) {
tree random_const, fld_t = TREE_TYPE(fld);
/* don't bother with noreturn functions for now */ if (TREE_THIS_VOLATILE(current_function_decl)) returnfalse;
/* gcc-4.5 doesn't discover some trivial noreturn functions */ if (EDGE_COUNT(EXIT_BLOCK_PTR_FOR_FN(cfun)->preds) == 0) returnfalse;
list = DECL_ATTRIBUTES(current_function_decl); return lookup_attribute("latent_entropy", list) != NULL_TREE;
}
static tree create_var(tree type, constchar *name)
{
tree var;
var = create_tmp_var(type, name);
add_referenced_var(var);
mark_sym_for_renaming(var); return var;
}
/* * Set up the next operation and its constant operand to use in the latent * entropy PRNG. When RHS is specified, the request is for perturbing the * local latent entropy variable, otherwise it is for perturbing the global * latent entropy variable where the two operands are already given by the * local and global latent entropy variables themselves. * * The operation is one of add/xor/rol when instrumenting the local entropy * variable and one of add/xor when perturbing the global entropy variable. * Rotation is not used for the latter case because it would transmit less * entropy to the global variable than the other two operations.
*/ staticenum tree_code get_op(tree *rhs)
{ staticenum tree_code op; unsigned HOST_WIDE_INT random_const;
random_const = get_random_const();
switch (op) { case BIT_XOR_EXPR:
op = PLUS_EXPR; break;
case PLUS_EXPR: if (rhs) {
op = LROTATE_EXPR; /* * This code limits the value of random_const to * the size of a long for the rotation
*/
random_const %= TYPE_PRECISION(long_unsigned_type_node); break;
}
case LROTATE_EXPR: default:
op = BIT_XOR_EXPR; break;
} if (rhs)
*rhs = build_int_cstu(long_unsigned_type_node, random_const); return op;
}
static gimple create_assign(enum tree_code code, tree lhs, tree op1,
tree op2)
{ return gimple_build_assign_with_ops(code, lhs, op1, op2);
}
staticvoid perturb_local_entropy(basic_block bb, tree local_entropy)
{
gimple_stmt_iterator gsi;
gimple assign;
tree rhs; enum tree_code op;
FOR_EACH_EDGE(e, ei, last_bb_e->src->preds) { if (ENTRY_BLOCK_PTR_FOR_FN(cfun) == e->src) continue; if (EXIT_BLOCK_PTR_FOR_FN(cfun) == e->src) continue;
FOR_EACH_VARIABLE(node) {
tree name, var = NODE_DECL(node);
if (DECL_NAME_LENGTH(var) < sizeof("latent_entropy") - 1) continue;
name = DECL_NAME(var); if (strcmp(IDENTIFIER_POINTER(name), "latent_entropy")) continue;
latent_entropy_decl = var; break;
}
return latent_entropy_decl != NULL_TREE;
}
staticunsignedint latent_entropy_execute(void)
{
basic_block bb;
tree local_entropy;
if (!create_latent_entropy_decl()) return 0;
/* prepare for step 2 below */
gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)); if (!single_pred_p(bb)) {
split_edge(single_succ_edge(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
}
/* 1. create the local entropy variable */
local_entropy = create_var(long_unsigned_type_node, "local_entropy");
/* 2. initialize the local entropy variable */
init_local_entropy(bb, local_entropy);
bb = bb->next_bb;
/* * 3. instrument each BB with an operation on the * local entropy variable
*/ while (bb != EXIT_BLOCK_PTR_FOR_FN(cfun)) {
perturb_local_entropy(bb, local_entropy);
bb = bb->next_bb;
}
/* 4. mix local entropy into the global entropy variable */
perturb_latent_entropy(local_entropy); return 0;
}
staticvoid latent_entropy_start_unit(void *gcc_data __unused, void *user_data __unused)
{
tree type, id; int quals;
if (in_lto_p) return;
/* extern volatile unsigned long latent_entropy */
quals = TYPE_QUALS(long_unsigned_type_node) | TYPE_QUAL_VOLATILE;
type = build_qualified_type(long_unsigned_type_node, quals);
id = get_identifier("latent_entropy");
latent_entropy_decl = build_decl(UNKNOWN_LOCATION, VAR_DECL, id, type);
/* * Call get_random_seed() with noinit=true, so that this returns * 0 in the case where no seed has been passed via -frandom-seed.
*/
deterministic_seed = get_random_seed(true);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.