Skip to content

Commit 4f0088a

Browse files
committed
fixed some sysinfo bugs, and added environment variable privacy
1 parent 47936fb commit 4f0088a

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

src/main/java/com/falsepattern/jfunge/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static void main(String[] args) throws IOException, ParseException {
4343
options.addOptionGroup(masterGroup);
4444
options.addOption(null, "trefunge", false, "Enable 3D (Trefunge) mode. By default, the interpreter emulates 2D Befunge for compatibility.");
4545
options.addOption("t", "concurrent", false, "Enables the Concurrent Funge extension (t instruction). Buggy programs can potentially forkbomb the interpreter.");
46+
options.addOption(null, "env", false, "Allows the interpreter to access the environment variables of the host system.");
4647
options.addOption(null, "syscall", false, "Enables the syscall feature (= instruction). This is a very dangerous permission to grant, it can call any arbitrary program on your system.");
4748
options.addOption(Option.builder("i")
4849
.longOpt("readperm")
@@ -105,6 +106,7 @@ public static void main(String[] args) throws IOException, ParseException {
105106
val featureSet = FeatureSet.builder();
106107
featureSet.trefunge(cmd.hasOption("trefunge"));
107108
featureSet.concurrent(cmd.hasOption("t"));
109+
featureSet.environment(cmd.hasOption("env"));
108110
featureSet.allowedInputFiles(cmd.getOptionValues("i"));
109111
featureSet.allowedOutputFiles(cmd.getOptionValues("o"));
110112
featureSet.perl(cmd.hasOption("perl"));

src/main/java/com/falsepattern/jfunge/interpreter/FeatureSet.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class FeatureSet {
1111
public final String[] allowedInputFiles;
1212
public final String[] allowedOutputFiles;
1313
public final boolean concurrent;
14+
public final boolean environment;
1415
public final long maxIter;
1516

1617
public final boolean perl;

src/main/java/com/falsepattern/jfunge/interpreter/Interpreter.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.nio.file.Paths;
2626
import java.util.ArrayList;
2727
import java.util.Arrays;
28-
import java.util.Collections;
2928
import java.util.HashMap;
3029
import java.util.List;
3130
import java.util.Map;
@@ -75,6 +74,9 @@ public boolean writeFile(String file, byte[] data) throws IOException {
7574
private final boolean unrestrictedOutput;
7675
private final Path[] allowedOutputPaths;
7776

77+
@Getter
78+
private final Map<String, String> env;
79+
7880
private final TIntObjectMap<Map<String, Object>> globals = new TIntObjectHashMap<>();
7981

8082
private Integer exitCode = null;
@@ -132,6 +134,13 @@ public Interpreter(String[] args, InputStream input, OutputStream output, FileIO
132134
}
133135
envFlags = env;
134136

137+
if (featureSet.environment) {
138+
this.env = new HashMap<>(System.getenv());
139+
} else {
140+
this.env = new HashMap<>();
141+
}
142+
this.env.put("JFUNGE_ENV", featureSet.environment ? "PASS" : "BLOCK");
143+
135144
if (!featureSet.perl) {
136145
fingerprintBlackList.add(PERL.INSTANCE.code());
137146
}
@@ -298,11 +307,6 @@ public void step(IP ip) {
298307
} while (p == ' ');
299308
}
300309

301-
@Override
302-
public Map<String, String> env() {
303-
return Collections.unmodifiableMap(System.getenv());
304-
}
305-
306310
@Override
307311
public int input(boolean stagger) {
308312
var value = -1;

src/main/java/com/falsepattern/jfunge/interpreter/instructions/Funge98.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,17 +470,18 @@ public static void sysInfo(ExecutionContext ctx) {
470470
val n = s.pop();
471471
val sizes = ctx.IP().stackStack().stackSizes();
472472
//20 envs
473+
s.push(0);
473474
for (Map.Entry<String, String> entry : ctx.env().entrySet()) {
474475
String key = entry.getKey();
475476
String value = entry.getValue();
476477
s.pushString(key + "=" + value);
477478
}
478479
//19 args
480+
s.push(0);
481+
s.push(0);
479482
for (String s1 : ctx.args()) {
480483
s.pushString(s1);
481484
}
482-
s.push(0);
483-
s.push(0);
484485
//18 sizes of stack stack
485486
for (int size : sizes) {
486487
s.push(size);

0 commit comments

Comments
 (0)