| 108 | |
| 109 | |
| 110 | def _identity(path): |
| 111 | fd = _regular(path) |
| 112 | try: |
| 113 | before = os.fstat(fd) |
| 114 | if before.st_size > 1024 * 1024 * 1024: |
| 115 | _error("resource_limit", "executable exceeds 1GiB identity limit") |
| 116 | digest = hashlib.sha256() |
| 117 | size = 0 |
| 118 | while True: |
| 119 | part = os.read(fd, 65536) |
| 120 | if not part: |
| 121 | break |
| 122 | size += len(part) |
| 123 | if size > 1024 * 1024 * 1024: |
| 124 | _error("resource_limit", "executable grew beyond identity limit") |
| 125 | digest.update(part) |
| 126 | if _signature(before) != _signature(os.fstat(fd)) or size != before.st_size: |
| 127 | _error("identity", "executable changed while hashing") |
| 128 | if not os.access(path, os.X_OK): |
| 129 | _error("input", "executable is not executable") |
| 130 | return {"path": path, "sha256": digest.hexdigest(), "bytes": size} |
| 131 | finally: |
| 132 | os.close(fd) |
| 133 | |
| 134 | |
| 135 | def _pairs(values): |