Skip to content

Commit 3eaa2cd

Browse files
committed
Check fd range in select()
1 parent 9a8fcd3 commit 3eaa2cd

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SelectModuleBuiltins.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
import static com.oracle.graal.python.nodes.ErrorMessages.INVALID_VALUE_NAN;
4444
import static com.oracle.graal.python.nodes.ErrorMessages.TOO_LARGE_TO_CONVERT_TO;
45+
import static com.oracle.graal.python.runtime.PosixSupportLibrary.FD_SETSIZE;
46+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
4547

4648
import java.util.List;
4749

@@ -177,7 +179,7 @@ private PList toList(boolean[] result, ObjAndFDList fds) {
177179
return factory().createList(PythonUtils.arrayCopyOf(resultObjs, resultObjsIdx));
178180
}
179181

180-
private static ObjAndFDList seq2set(VirtualFrame frame, Object sequence, PythonObjectLibrary sequenceLib, PythonObjectLibrary itemLib, LookupAndCallBinaryNode callGetItemNode,
182+
private ObjAndFDList seq2set(VirtualFrame frame, Object sequence, PythonObjectLibrary sequenceLib, PythonObjectLibrary itemLib, LookupAndCallBinaryNode callGetItemNode,
181183
FastConstructListNode constructListNode, PosixResources resources) {
182184
PArguments.ThreadState threadState = PArguments.getThreadState(frame);
183185
// We cannot assume any size of those two arrays, because the sequence may change as a
@@ -191,6 +193,9 @@ private static ObjAndFDList seq2set(VirtualFrame frame, Object sequence, PythonO
191193
Object pythonObject = callGetItemNode.executeObject(frame, pSequence, i);
192194
objects.add(pythonObject);
193195
int fd = itemLib.asFileDescriptorWithState(pythonObject, threadState);
196+
if (fd >= FD_SETSIZE) {
197+
throw raise(ValueError, ErrorMessages.FILE_DESCRIPTOR_OUT_OF_RANGE_IN_SELECT);
198+
}
194199
fds.add(fd);
195200
containsSocket |= resources.isSocket(fd);
196201
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ public abstract class ErrorMessages {
257257
public static final String FILE_OR_STREAM_IS_NOT_READABLE = "File or stream is not readable.";
258258
public static final String FILE_OR_STREAM_IS_NOT_SEEKABLE = "File or stream is not seekable.";
259259
public static final String FILE_OR_STREAM_IS_NOT_WRITABLE = "File or stream is not writable.";
260+
public static final String FILE_DESCRIPTOR_OUT_OF_RANGE_IN_SELECT = "filedescriptor out of range in select()";
260261
public static final String FILL_CHAR_MUST_BE_LENGTH_1 = "The fill character must be exactly one character long";
261262
public static final String FILTER_SPEC_MUST_BE_DICT = "Filter specifier must be a dict or dict-like object";
262263
public static final String FILTER_SPECIFIER_MUST_HAVE = "Filter specifier must have an \"id\" entry";

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PosixSupportLibrary.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ public abstract class PosixSupportLibrary extends Library {
114114
public static final int PROT_EXEC = 0x4; /* Page can be executed. */
115115
public static final int PROT_NONE = 0x0; /* Page can not be accessed. */
116116

117+
public static final int FD_SETSIZE = 1024;
118+
117119
public abstract String getBackend(Object recevier);
118120

119121
public abstract String strerror(Object receiver, int errorCode);

0 commit comments

Comments
 (0)