with self.assertRaises(TypeError):
sock.sendmsg_afalg(op=socket.ALG_OP_ENCRYPT, assoclen=-1)
+ def test_length_restriction(self):
+ # bpo-35050, off-by-one error in length check
+ sock = socket.socket(socket.AF_ALG, socket.SOCK_SEQPACKET, 0)
+ self.addCleanup(sock.close)
+
+ # salg_type[14]
+ with self.assertRaises(FileNotFoundError):
+ sock.bind(("t" * 13, "name"))
+ with self.assertRaisesRegex(ValueError, "type too long"):
+ sock.bind(("t" * 14, "name"))
+
+ # salg_name[64]
+ with self.assertRaises(FileNotFoundError):
+ sock.bind(("type", "n" * 63))
+ with self.assertRaisesRegex(ValueError, "name too long"):
+ sock.bind(("type", "n" * 64))
+
+
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
class TestMSWindowsTCPFlags(unittest.TestCase):
knownTCPFlags = {
{
return 0;
}
- /* sockaddr_alg has fixed-sized char arrays for type and name */
- if (strlen(type) > sizeof(sa->salg_type)) {
+ /* sockaddr_alg has fixed-sized char arrays for type, and name
+ * both must be NULL terminated.
+ */
+ if (strlen(type) >= sizeof(sa->salg_type)) {
PyErr_SetString(PyExc_ValueError, "AF_ALG type too long.");
return 0;
}
strncpy((char *)sa->salg_type, type, sizeof(sa->salg_type));
- if (strlen(name) > sizeof(sa->salg_name)) {
+ if (strlen(name) >= sizeof(sa->salg_name)) {
PyErr_SetString(PyExc_ValueError, "AF_ALG name too long.");
return 0;
}