From d9f58253e30ea80e57d8f54e41e9cd114cc13973 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 15 Feb 2010 21:27:37 -0800 Subject: Use clamping to more easily normalize input values The kernel has min/max/clamp macros to make range comparisons easier. Clamp is a newer invention, but we can define it for older kernels in terms of min and max. --- tapset/conversions.stp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'tapset/conversions.stp') diff --git a/tapset/conversions.stp b/tapset/conversions.stp index ec990aed..3bbbb725 100644 --- a/tapset/conversions.stp +++ b/tapset/conversions.stp @@ -35,8 +35,7 @@ deref_fault: /* branched to from deref_string() */ */ function kernel_string_n:string (addr:long, n:long) %{ /* pure */ char *destination = THIS->__retvalue; - long len = THIS->n + 1; - len = (len > MAXSTRINGLEN) ? MAXSTRINGLEN : len; + int64_t len = clamp_t(int64_t, THIS->n + 1, 1, MAXSTRINGLEN); deref_string (destination, THIS->addr, len); if (0) { deref_fault: /* branched to from deref_string() */ @@ -210,9 +209,8 @@ function user_string_n:string (addr:long, n:long) { * the rare cases when userspace data is not accessible at the given address. */ function user_string_n2:string (addr:long, n:long, err_msg:string) %{ /* pure */ /* unprivileged */ - long len = THIS->n + 1; + int64_t len = clamp_t(int64_t, THIS->n + 1, 1, MAXSTRINGLEN); assert_is_myproc(); - len = (len > MAXSTRINGLEN) ? MAXSTRINGLEN : len; if (_stp_strncpy_from_user(THIS->__retvalue, (char __user *) (uintptr_t) THIS->addr, len) < 0) @@ -232,11 +230,10 @@ function user_string_n2:string (addr:long, n:long, err_msg:string) %{ /* pure */ * about the failure. */ function user_string_n_warn:string (addr:long, n:long) %{ /* pure */ /* unprivileged */ - long len = THIS->n + 1; + int64_t len = clamp_t(int64_t, THIS->n + 1, 1, MAXSTRINGLEN); long rc; assert_is_myproc(); - len = (len > MAXSTRINGLEN) ? MAXSTRINGLEN : len; rc = _stp_strncpy_from_user(THIS->__retvalue, (char __user *) (uintptr_t) THIS->addr, len); if (rc < 0) { @@ -264,9 +261,8 @@ function user_string_n_warn:string (addr:long, n:long) %{ /* pure */ /* unprivil * address. */ function user_string_n_quoted:string (addr:long, n:long) %{ /* pure */ /* unprivileged */ - long len; + int64_t len = clamp_t(int64_t, THIS->n + 1, 1, MAXSTRINGLEN); assert_is_myproc(); - len = THIS->n + 1; if (THIS->addr == 0) strlcpy(THIS->__retvalue, "NULL", MAXSTRINGLEN); else -- cgit