Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Wasm sources and sinks #4

Open
wants to merge 27 commits into
base: primitaint-merge
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b193ae5
added wasm tainting
0drai Aug 2, 2024
b7113c2
Revert "deactivate unneded taint sources"
0drai Aug 2, 2024
3bbfdfe
some refactoring
0drai Aug 2, 2024
893edc3
fixed optimized switch cases with tainted discriminants
0drai Aug 4, 2024
912d3ab
Added taint propogation to Math.round
alexbara2000 Aug 1, 2024
7fa12b5
Added ability to write a primitive value to an object. This is needed…
alexbara2000 Aug 1, 2024
ddd2984
Added taint propagation to most of the JSMath library
alexbara2000 Aug 1, 2024
bf46a06
Finished making JSMath taint aware
alexbara2000 Aug 1, 2024
2cace44
refactoring
alexbara2000 Aug 1, 2024
52e79d4
Ensured the result of an operation is stored in a new object
alexbara2000 Aug 1, 2024
dd63564
Added base tests for JS math library testing
alexbara2000 Aug 1, 2024
0b3c90d
Added more comprehensive tests
alexbara2000 Aug 1, 2024
64b409e
Fixed min and max logic
alexbara2000 Aug 1, 2024
a98915c
simplified if statement with a helper function
alexbara2000 Aug 2, 2024
5edeb2a
refactored math imul and atan2
alexbara2000 Aug 5, 2024
390034b
Fixes Array.indexOf/includes for tainted numbers
leeN Aug 7, 2024
6670014
added wasm tainting
0drai Aug 2, 2024
b5b394c
small fix in typedarray
0drai Aug 7, 2024
1de497f
removed conflict headers
0drai Aug 8, 2024
b1e7e79
removed conflict headers
0drai Aug 8, 2024
ed70f3c
Fixed equality of tainted numbers
leeN Aug 8, 2024
e9e49a1
Merge pull request #1 from leeN/primitaint-wasm-equality
0drai Aug 11, 2024
a22ae64
removed baselineinterpr and buggy taint sources
0drai Aug 13, 2024
58d1195
Revert "removed baselineinterpr and buggy taint sources"
0drai Aug 14, 2024
4281052
fixed taint reporting issue in worker
0drai Aug 15, 2024
440567b
create seperate taint object when reporting taint sinks
0drai Aug 21, 2024
8f249c5
added taint object to report taint sink in worker
0drai Aug 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions gfx/qcms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
#![allow(non_upper_case_globals)]
// These are needed for the neon SIMD code and can be removed once the MSRV supports the
// instrinsics we use
#![cfg_attr(feature = "neon", feature(stdsimd))]
#![cfg_attr(
feature = "neon",
feature(arm_target_feature, raw_ref_op)

)]
// TODO(0drai): Cannot build taintfox unless this is removed :/
//#![cfg_attr(feature = "neon", feature(stdsimd))]
//#![cfg_attr(
// feature = "neon",
// feature(arm_target_feature, raw_ref_op)
//
//)]

/// These values match the Rendering Intent values from the ICC spec
#[repr(C)]
Expand Down
2 changes: 1 addition & 1 deletion js/public/experimental/TypedData.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ extern JS_PUBLIC_API JSObject* UnwrapReadableStream(JSObject* obj);
namespace detail {

constexpr size_t TypedArrayLengthSlot = 1;
constexpr size_t TypedArrayDataSlot = 3;
constexpr size_t TypedArrayDataSlot = 4;

} // namespace detail

Expand Down
27 changes: 22 additions & 5 deletions js/src/builtin/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "vm/JSContext.h"
#include "vm/JSFunction.h"
#include "vm/JSObject.h"
#include "vm/NumberObject.h"
#include "vm/PlainObject.h" // js::PlainObject
#include "vm/SelfHosting.h"
#include "vm/Shape.h"
Expand Down Expand Up @@ -3898,6 +3899,8 @@ static bool array_slice(JSContext* cx, unsigned argc, Value* vp) {

/* Step 12. */
args.rval().setObject(*arr);
//TODO(SAM) - Trace the array object
/*arr->as<ArrayObject>()*/
return true;
}

Expand Down Expand Up @@ -4087,7 +4090,6 @@ enum class SearchKind {
// semantics are used.
Includes,
};

template <SearchKind Kind, typename Iter>
static bool SearchElementDense(JSContext* cx, HandleValue val, Iter iterator,
MutableHandleValue rval) {
Expand Down Expand Up @@ -4118,8 +4120,15 @@ static bool SearchElementDense(JSContext* cx, HandleValue val, Iter iterator,
}

// Fast path for numbers.
if (val.isNumber()) {
double dval = val.toNumber();
if (val.isNumber() || isTaintedNumber(val)) {
double dval;
if(isTaintedNumber(val)) {
if (!ToNumber(cx, val, &dval)) {
return false;
}
} else {
dval = val.toNumber();
}
// For |includes|, two NaN values are considered equal, so we use a
// different implementation for NaN.
if (Kind == SearchKind::Includes && std::isnan(dval)) {
Expand All @@ -4129,8 +4138,15 @@ static bool SearchElementDense(JSContext* cx, HandleValue val, Iter iterator,
};
return iterator(cx, cmp, rval);
}
auto cmp = [dval](JSContext*, const Value& element, bool* equal) {
*equal = (element.isNumber() && element.toNumber() == dval);
auto cmp = [dval](JSContext* context, const Value& element, bool* equal) {
if(isTaintedNumber(element)) {
NumberObject* obj = &element.toObject().as<NumberObject>();
double x = obj->unbox();;

*equal = x == dval;
} else {
*equal = (element.isNumber() && element.toNumber() == dval);
}
return true;
};
return iterator(cx, cmp, rval);
Expand Down Expand Up @@ -4960,6 +4976,7 @@ static inline bool ArrayConstructorImpl(JSContext* cx, CallArgs& args,

/* ES5 15.4.2 */
bool js::ArrayConstructor(JSContext* cx, unsigned argc, Value* vp) {
//TODO(SAM) - Trace the array object
AutoJSConstructorProfilerEntry pseudoFrame(cx, "Array");
CallArgs args = CallArgsFromVp(argc, vp);
return ArrayConstructorImpl(cx, args, /* isConstructor = */ true);
Expand Down
19 changes: 19 additions & 0 deletions js/src/builtin/Boolean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "vm/JSObject.h"

#include "vm/BooleanObject-inl.h"
#include "vm/NumberObject-inl.h"

using namespace js;

Expand Down Expand Up @@ -109,6 +110,16 @@ static const JSFunctionSpec boolean_methods[] = {
static bool Boolean(JSContext* cx, unsigned argc, Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);

//TODO(0drai): For cases where the Boolean constructor is used
//e.g., Boolean(taintedValue)

if (isTaintedNumber(args[0])){
// NOTE(0drai): Save since isTainted* checks for type
double d = args[0].toObject().as<NumberObject>().unbox();
JSObject *v = NumberObject::create(cx, d);
args[0].setObject(*v);
}

// Step 1.
bool b = args.length() != 0 ? JS::ToBoolean(args[0]) : false;

Expand Down Expand Up @@ -173,5 +184,13 @@ JS_PUBLIC_API bool js::ToBooleanSlow(HandleValue v) {
#endif

MOZ_ASSERT(v.isObject());
// Workaround for #216
if (isTaintedNumber(v)) {
// NOTE(0drai): Save since isTainted* checks for type
double d = v.toObject().as<NumberObject>().unbox();
if (std::isnan(d)) return false;
return d != 0;
}

return !EmulatesUndefined(&v.toObject());
}
8 changes: 8 additions & 0 deletions js/src/builtin/DataViewObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ DataViewObject* DataViewObject::create(
return nullptr;
}

obj->setReservedSlot(TAINT_SLOT, PrivateValue(nullptr));

if (arrayBuffer && arrayBuffer->isWasm()) {
obj->setTaint(
TaintFlow(TaintOperationFromContext(cx, "WASM Array taint source", true)));
}

return obj;
}

Expand Down Expand Up @@ -1017,6 +1024,7 @@ const JSPropertySpec DataViewObject::properties[] = {
JS_PSG("buffer", DataViewObject::bufferGetter, 0),
JS_PSG("byteLength", DataViewObject::byteLengthGetter, 0),
JS_PSG("byteOffset", DataViewObject::byteOffsetGetter, 0),
JS_PSG("taint", js::Array_taintGetter, JSPROP_PERMANENT),
JS_STRING_SYM_PS(toStringTag, "DataView", JSPROP_READONLY), JS_PS_END};

JS_PUBLIC_API JSObject* JS_NewDataView(JSContext* cx, HandleObject buffer,
Expand Down
46 changes: 46 additions & 0 deletions js/src/builtin/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,51 @@ js::str_tainted(JSContext* cx, unsigned argc, Value* vp)
return true;
}

bool js::str_taintedFromArray(JSContext* cx, unsigned argc, Value* vp)
{
// String.taintedFromArray(string, operation, array, array.taint.length)
CallArgs args = CallArgsFromVp(argc, vp);

RootedString str(cx, ArgToLinearString(cx, args, 0));
if (!str || str->length() == 0) {
return false;
}

RootedString opName(cx, args[1].toString());
if (!opName) {
return false;
}

UniqueChars op_chars = JS_EncodeStringToUTF8(cx, opName);
if (!op_chars) {
return false;
}

TaintFlow op = TaintFlow(TaintOperationFromContext(cx,op_chars.get(), true));
TaintFlow arrayTaintFlow = JS::getValueTaint(args[2]);
TaintFlow combined = TaintFlow::append(arrayTaintFlow, op);

double taintFlowSize = 0;

if (!ToInteger(cx, args[3], &taintFlowSize)) {
return false;
}

SafeStringTaint taint(combined, taintFlowSize + 1);

JSString* tainted_str = NewDependentString(cx, str, 0, str->length());
if (!tainted_str) {
return false;
}

tainted_str->setTaint(taint);

MOZ_ASSERT(tainted_str->isTainted());

args.rval().setString(tainted_str);
return true;
}

/*
* TaintFox: taint property implementation.
*
Expand Down Expand Up @@ -4508,6 +4553,7 @@ static const JSFunctionSpec string_static_methods[] = {

// TaintFox: Helper function for manual taint sources.
JS_FN("tainted", str_tainted, 1,0),
JS_FN("taintedFromArray", str_taintedFromArray, 4,0),

JS_FS_END};

Expand Down
2 changes: 2 additions & 0 deletions js/src/builtin/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ extern bool str_fromCodePoint_one_arg(JSContext* cx, HandleValue code,
// TaintFox: Exported for the js shell: taint(str).
bool str_tainted(JSContext* cx, unsigned argc, Value* vp);

bool str_taintedFromArray(JSContext* cx, unsigned argc, Value* vp);

extern bool str_includes(JSContext* cx, unsigned argc, Value* vp);

extern bool str_indexOf(JSContext* cx, unsigned argc, Value* vp);
Expand Down
66 changes: 64 additions & 2 deletions js/src/builtin/TypedArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,15 @@ function TypedArrayIndexOf(searchElement, fromIndex = 0) {

// Steps 11.b.i-iii.
if (O[k] === searchElement) {
// Taintfox: add taint to the result if the searchElement is tainted....
if (searchElement?.taint?.length > 0) {
k = AddTaintOperationToNumberFromNumber(O, k, "indexOf", searchElement);
}

//... or the source array
else if (O?.taint?.length > 0){
//TODO(0drai): implement
}
return k;
}
}
Expand Down Expand Up @@ -677,6 +686,11 @@ function TypedArrayJoin(separator) {
R += sep + ToString(element);
}

// Taintfox: add taint to the result if the source array is tainted
if (O && O.taint?.length > 0){
AddTaintOperationNative(R, "join", O.taint[0]);
}

// Step 9.
return R;
}
Expand Down Expand Up @@ -1029,6 +1043,14 @@ function TypedArraySlice(start, end) {
}
}

// Taintfox: add taint to the result if the source array
// or the start index is tainted
if (start && start.taint?.length > 0) {
AddTaintOperationToArray(A, "slice", start);
} else if (O && O.taint?.length > 0){
AddTaintOperationToArray(A, "slice", O);
}

// Step 16.
return A;
}
Expand Down Expand Up @@ -1282,12 +1304,23 @@ function TypedArraySubarray(begin, end) {
var beginByteOffset = srcByteOffset + beginIndex * elementSize;

// Steps 15-16.
return TypedArraySpeciesCreateWithBuffer(
var result = TypedArraySpeciesCreateWithBuffer(
obj,
buffer,
beginByteOffset,
newLength
);

// Taintfox: add taint to the result if the sliced array or any other parameter is tainted
if (begin?.taint?.length > 0) {
AddTaintOperationToArray(result, "subarray", begin);
} else if (end?.taint?.length > 0) {
AddTaintOperationToArray(result, "subarray", end);
} else if (obj?.taint?.length > 0){
AddTaintOperationToArray(result, "subarray", obj);
}

return result;
}

// https://tc39.es/proposal-relative-indexing-method
Expand Down Expand Up @@ -1327,6 +1360,8 @@ function TypedArrayAt(index) {
return undefined;
}

//ReportWasmTaintSink(obj, obj[k]);

// Step 8.
return obj[k];
}
Expand Down Expand Up @@ -1562,6 +1597,11 @@ function TypedArrayStaticFrom(source, mapfn = undefined, thisArg = undefined) {
targetObj[k] = source[k];
}

// Taintfox: add taint to the result if the source array is tainted
if (source?.taint?.length > 0){
AddTaintOperationToArray(targetObj, "from", source);
}

// Step 7.g.
return targetObj;
}
Expand All @@ -1578,6 +1618,10 @@ function TypedArrayStaticFrom(source, mapfn = undefined, thisArg = undefined) {
// Steps 7.a, 7.d-f.
TypedArrayInitFromPackedArray(targetObj, source);

if (source?.taint?.length > 0){
AddTaintOperationToArray(targetObj, "from", source);
}

// Step 7.g.
return targetObj;
}
Expand Down Expand Up @@ -1611,6 +1655,9 @@ function TypedArrayStaticFrom(source, mapfn = undefined, thisArg = undefined) {
// the list's start in the loop above. That would introduce unacceptable overhead.
// Additionally, the loop's logic is simple enough not to require the assert.

if (source?.taint?.length > 0){
AddTaintOperationToArray(targetObj, "from", source);
}
// Step 7.g.
return targetObj;
}
Expand Down Expand Up @@ -1639,6 +1686,9 @@ function TypedArrayStaticFrom(source, mapfn = undefined, thisArg = undefined) {

// Step 13.e.
targetObj[k] = mappedValue;
if (kValue?.taint?.length > 0){
AddTaintOperationToArray(targetObj, "from", kValue);
}
}

// Step 14.
Expand All @@ -1664,9 +1714,16 @@ function TypedArrayStaticOf(/*...items*/) {
// Step 5.
var newObj = TypedArrayCreateWithLength(C, len);

var value = null;

// Steps 6-7.
for (var k = 0; k < len; k++) {
newObj[k] = GetArgument(k);
value = GetArgument(k);
newObj[k] = value;
// Taintfox: add taint to the result if any item is tainted
if (value?.taint?.length > 0){
AddTaintOperationToArray(newObj, "of", value);
}
}

// Step 8.
Expand Down Expand Up @@ -1978,6 +2035,11 @@ function TypedArrayToReversed() {
A[k] = fromValue;
}

if (O?.taint?.length > 0){
// Taintfox: add taint to the result if the source array is tainted
AddTaintOperationToArray(A, "reversed", O);
}

// Step 7. Return A.
return A;
}
Expand Down
Loading